-
Notifications
You must be signed in to change notification settings - Fork 67
/
auth.go
43 lines (33 loc) · 952 Bytes
/
auth.go
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
package auth
import (
"context"
)
type TenantID string
type UserID string
const (
AnonymousTenantID TenantID = "tenant_000000000000000000000000001"
AnonymousUserID UserID = "user_000000000000000000000000001"
)
type Identity struct {
TenantID TenantID
UserID UserID
}
type identityKey struct{}
func IdentityFromContext(ctx context.Context) Identity {
ident, ok := ctx.Value(identityKey{}).(Identity)
if !ok {
return Identity{TenantID: AnonymousTenantID, UserID: AnonymousUserID}
}
return ident
}
func ContextWithIdentity(ctx context.Context, ident Identity) context.Context {
return context.WithValue(ctx, identityKey{}, ident)
}
type authTokenKey struct{}
func AuthTokenFromContext(ctx context.Context) (string, bool) {
token, ok := ctx.Value(authTokenKey{}).(string)
return token, ok
}
func ContextWithAuthToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, authTokenKey{}, token)
}