File size: 6,319 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package permission

import (
	"context"
	"errors"
	"regexp"
	"strings"
	"sync/atomic"

	"github.com/google/uuid"
	"github.com/target/goalert/util/log"
)

// SourceContext will return a context with the provided SourceInfo.
func SourceContext(ctx context.Context, src *SourceInfo) context.Context {
	if src == nil {
		return ctx
	}
	// make a copy, so it's read-only
	dup := *src
	ctx = log.WithField(ctx, "AuthSource", src.String())
	return context.WithValue(ctx, contextKeySourceInfo, dup)
}

// Source will return the SourceInfo associated with a context.
func Source(ctx context.Context) *SourceInfo {
	src, ok := ctx.Value(contextKeySourceInfo).(SourceInfo)
	if !ok {
		return nil
	}
	return &src
}

// UserSourceContext behaves like UserContext, but provides SourceInfo about the authorization.
func UserSourceContext(ctx context.Context, id string, r Role, src *SourceInfo) context.Context {
	ctx = SourceContext(ctx, src)
	ctx = UserContext(ctx, id, r)
	return ctx
}

// UserContext will return a context authenticated with the users privileges.
func UserContext(ctx context.Context, id string, r Role) context.Context {
	id = strings.ToLower(id)
	ctx = context.WithValue(ctx, contextHasAuth, 1)
	ctx = context.WithValue(ctx, contextKeyUserID, id)
	ctx = ensureAuthCheckCountContext(ctx)
	ctx = context.WithValue(ctx, contextKeyUserRole, r)
	ctx = log.WithField(ctx, "AuthUserID", id)
	return ctx
}

var sysRx = regexp.MustCompile(`^([a-zA-Z0-9]+|Sudo\[[a-zA-Z0-9]+\])$`)

// SystemContext will return a new context with the system privileges.
// Name must be alphanumeric.
func SystemContext(ctx context.Context, componentName string) context.Context {
	if !sysRx.MatchString(componentName) {
		panic(errors.New("invalid system component name: " + componentName))
	}
	ctx = context.WithValue(ctx, contextHasAuth, 1)
	ctx = context.WithValue(ctx, contextKeySystem, componentName)
	ctx = AuthCheckCountContext(ctx, 0)
	ctx = log.WithField(ctx, "AuthSystemComponent", componentName)
	return ctx
}

// AuthCheckCount will return the current number of authorization checks as
// well as the maximum.
func AuthCheckCount(ctx context.Context) (value, max uint64) {
	val, ok := ctx.Value(contextKeyCheckCount).(*uint64)
	if ok {
		value = atomic.LoadUint64(val)
	}

	max, _ = ctx.Value(contextKeyCheckCountMax).(uint64)

	return value, max
}

func ensureAuthCheckCountContext(ctx context.Context) context.Context {
	_, ok := ctx.Value(contextKeyCheckCount).(*uint64)
	if !ok {
		return AuthCheckCountContext(ctx, 0)
	}
	return ctx
}

// AuthCheckCountContext will return a new context with the AuthCheckCount maximum
// set to the provided value. If max is 0, there will be no limit.
func AuthCheckCountContext(ctx context.Context, max uint64) context.Context {
	val, _ := ctx.Value(contextKeyCheckCount).(*uint64)
	if val == nil {
		ctx = context.WithValue(ctx, contextKeyCheckCount, new(uint64))
	}
	ctx = context.WithValue(ctx, contextKeyCheckCountMax, max)

	return ctx
}

// ServiceSourceContext behaves like ServiceContext, but provides SourceInfo about the authorization.
func ServiceSourceContext(ctx context.Context, id string, src *SourceInfo) context.Context {
	ctx = SourceContext(ctx, src)
	ctx = ServiceContext(ctx, id)
	return ctx
}

// ServiceContext will return a new context with privileges for the given service.
func ServiceContext(ctx context.Context, serviceID string) context.Context {
	serviceID = strings.ToLower(serviceID)
	ctx = context.WithValue(ctx, contextHasAuth, 1)
	ctx = ensureAuthCheckCountContext(ctx)
	ctx = context.WithValue(ctx, contextKeyServiceID, serviceID)
	ctx = log.WithField(ctx, "AuthServiceID", serviceID)

	return ctx
}

// TeamContext will return a new context with privileges for the given team.
func TeamContext(ctx context.Context, teamID string) context.Context {
	teamID = strings.ToLower(teamID)
	ctx = context.WithValue(ctx, contextHasAuth, 1)
	ctx = context.WithValue(ctx, contextKeyCheckCount, new(uint64))
	ctx = context.WithValue(ctx, contextKeyTeamID, teamID)
	ctx = log.WithField(ctx, "AuthTeamID", teamID)

	return ctx
}

// WithoutAuth returns a context will all auth info stripped out.
func WithoutAuth(ctx context.Context) context.Context {
	if System(ctx) {
		ctx = context.WithValue(ctx, contextKeySystem, nil)
	}
	if id, ok := ctx.Value(contextKeyUserID).(string); ok && id != "" {
		ctx = context.WithValue(ctx, contextKeyUserID, nil)
		ctx = context.WithValue(ctx, contextKeyUserRole, nil)
	}
	if Service(ctx) {
		ctx = context.WithValue(ctx, contextKeyServiceID, nil)
	}

	v, _ := ctx.Value(contextHasAuth).(int)
	if v == 1 {
		ctx = context.WithValue(ctx, contextHasAuth, nil)
	}

	return ctx
}

// SudoContext elevates an existing context to system level. The elevated context is automatically canceled
// as soon as the callback returns.
func SudoContext(ctx context.Context, f func(context.Context)) {
	name := "Sudo"
	cname := SystemComponentName(ctx)
	if cname != "" {
		name += "[" + cname + "]"
	}
	sCtx, cancel := context.WithCancel(SystemContext(ctx, name))
	defer cancel()
	f(sCtx)
}

// UserID will return the UserID associated with a context.
func UserID(ctx context.Context) string {
	uid, _ := ctx.Value(contextKeyUserID).(string)
	return uid
}

// UserNullUUID will return the UserID associated with a context as a NullUUID.
func UserNullUUID(ctx context.Context) uuid.NullUUID {
	if id, err := uuid.Parse(UserID(ctx)); err == nil {
		return uuid.NullUUID{UUID: id, Valid: true}
	}

	return uuid.NullUUID{}
}

// ServiceNullUUID will return the ServiceID associated with a context as a NullUUID.
func ServiceNullUUID(ctx context.Context) uuid.NullUUID {
	if id, err := uuid.Parse(ServiceID(ctx)); err == nil {
		return uuid.NullUUID{UUID: id, Valid: true}
	}

	return uuid.NullUUID{}
}

// SystemComponentName will return the component name used to initiate a context.
func SystemComponentName(ctx context.Context) string {
	name, _ := ctx.Value(contextKeySystem).(string)
	return name
}

// ServiceID will return the ServiceID associated with a context.
func ServiceID(ctx context.Context) string {
	sid, _ := ctx.Value(contextKeyServiceID).(string)
	return sid
}

// TeamID will return the TeamID associated with a context.
func TeamID(ctx context.Context) string {
	sid, _ := ctx.Value(contextKeyTeamID).(string)
	return sid
}