-
Notifications
You must be signed in to change notification settings - Fork 0
/
heimdall.go
156 lines (139 loc) · 3.57 KB
/
heimdall.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
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
package heimdall
import (
"context"
"errors"
"time"
)
var (
ErrNotFound = errors.New("Not Found")
ErrExpired = errors.New("Expired")
ErrInvalidCredentials = errors.New("Invalid Credentials")
)
const (
AuthorizationResponseTypeToken = "token"
AuthorizationResponseTypeCode = "code"
TokenGrantTypeAuthCode = "authorization_code"
TokenGrantTypeClientCredentials = "client_credentials"
TokenGrantTypeRefreshToken = "refresh_token"
TokenGrantTypePassword = "password"
TokenTypeBasic = "Basic"
TokenTypeSession = "Session"
TokenTypeBearer = "Bearer"
TokenTypeRefresh = "Refresh"
TokenTypeCode = "AuthorizationCode"
TokenTypeConcent = "UserConcent"
TokenAccessTypeOffline = "offline"
TokenAccessTypeOnline = "online"
)
type HeimdallDB interface {
CreateObj
TokenDB
UserDB
ClientDB
}
type CreateObj interface {
NewToken() Token
NewUser() User
NewClient() Client
}
type TokenDB interface {
CreateToken(token Token) (Token, error)
GetToken(tokenId string) (Token, error)
UpdateToken(token Token) (Token, error)
DeleteToken(tokenId string) error
}
type UserDB interface {
VerifyUser(username, password string) (User, error)
CreateUser(user User) (User, error)
GetUser(userId string) (User, error)
UpdateUser(user User) (User, error)
DeleteUser(userId string) error
}
type ClientDB interface {
VerifyClient(clientId, clientSecret string) (Client, error)
CreateClient(client Client) (Client, error)
GetClient(clientId string) (Client, error)
UpdateClient(client Client) (Client, error)
DeleteClient(clientId string) error
}
type Token interface {
GetId() string
SetId(id string)
GetType() string
SetType(t string)
GetUserId() string
SetUserId(userId string)
GetClientId() string
SetClientId(clientId string)
GetExpires() time.Time
SetExpires(expires time.Time)
GetScope() []string
SetScope(scope []string)
GetAccessType() string
SetAccessType(accessType string)
GetRefreshToken() string
SetRefreshToken(refreshToken string)
}
type User interface {
GetId() string
SetId(id string)
GetName() string
SetName(name string)
GetConcents(clientId string) []string
SetConcents(clientId string, concents []string)
}
type Client interface {
GetId() string
SetId(id string)
GetSecret() string
SetSecret(secret string)
GetName() string
SetName(name string)
GetType() string
SetType(t string)
GetInternal() bool
SetInternal(internal bool)
GetRedirectURIs() []string
SetRedirectURIs(redirectURIs []string)
}
type UserIder interface {
UserId(id string)
}
type ClientIder interface {
ClientId(id string)
}
func setValuesOnContext(ctx context.Context, userId string, clientId string) {
if uier := ctx.Value("userider"); uier != nil {
if uierv, ok := uier.(UserIder); ok {
uierv.UserId(userId)
}
}
if cier := ctx.Value("clientider"); cier != nil {
if cierv, ok := cier.(ClientIder); ok {
cierv.ClientId(clientId)
}
}
}
type ctxkey int
var tokenKey ctxkey = 0
var userKey ctxkey = 1
var clientKey ctxkey = 2
func newContext(ctx context.Context, t Token, u User, c Client) context.Context {
if t == nil || c == nil {
return ctx
}
tc := context.WithValue(ctx, tokenKey, t)
tc = context.WithValue(ctx, userKey, u)
tc = context.WithValue(ctx, clientKey, c)
return tc
}
func FromContext(ctx context.Context) (t Token, u User, c Client, ok bool) {
if t, ok = ctx.Value(tokenKey).(Token); !ok {
return
}
if c, ok = ctx.Value(clientKey).(Client); !ok {
return
}
u, _ = ctx.Value(userKey).(User)
return
}