/
context.go
74 lines (63 loc) · 1.49 KB
/
context.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
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
package auth
import (
"context"
ome "github.com/omecodes/libome"
pb "github.com/omecodes/store/gen/go/proto"
)
type ctxUser struct{}
type ctxApp struct{}
type ctxJWt struct{}
type ctxCredentialsManager struct{}
type ctxAuthenticationProviders struct{}
func ContextWithUser(parent context.Context, user *pb.User) context.Context {
return context.WithValue(parent, ctxUser{}, user)
}
func ContextWithApp(parent context.Context, clientApp *pb.ClientApp) context.Context {
return context.WithValue(parent, ctxApp{}, clientApp)
}
func Get(ctx context.Context) *pb.User {
o := ctx.Value(ctxUser{})
if o == nil {
return nil
}
return o.(*pb.User)
}
func App(ctx context.Context) *pb.ClientApp {
o := ctx.Value(ctxApp{})
if o == nil {
return nil
}
return o.(*pb.ClientApp)
}
func IsContextFromAuthorizedApp(ctx context.Context) bool {
return ctx.Value(ctxApp{}) != nil
}
func IsAdminAppFromContext(ctx context.Context) bool {
o := ctx.Value(ctxApp{})
if o == nil {
return false
}
a := o.(*pb.ClientApp)
return a != nil && a.AdminApp
}
func JWT(ctx context.Context) *ome.JWT {
o := ctx.Value(ctxJWt{})
if o == nil {
return nil
}
return o.(*ome.JWT)
}
func GetCredentialsManager(ctx context.Context) CredentialsManager {
o := ctx.Value(ctxCredentialsManager{})
if o == nil {
return nil
}
return o.(CredentialsManager)
}
func GetProviders(ctx context.Context) ProviderManager {
o := ctx.Value(ctxAuthenticationProviders{})
if o == nil {
return nil
}
return o.(ProviderManager)
}