-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
314 lines (272 loc) · 9.6 KB
/
jwt.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package auth
import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"github.com/coreos/dex/storage"
"github.com/coreos/go-oidc"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
errors2 "github.com/micro/go-micro/errors"
"github.com/micro/go-micro/metadata"
"go.uber.org/zap"
"golang.org/x/oauth2"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/auth/claim"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/auth"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/rest"
"github.com/pydio/cells/common/service/proto"
)
// Config is the config format for the main application.
type simpleConfig struct {
Issuer string `json:"issuer"`
// StaticClients cause the server to use this list of clients rather than
// querying the storage. Write operations, like creating a client, will fail.
StaticClients []storage.Client `json:"staticClients"`
}
func DefaultJWTVerifier() *JWTVerifier {
var dex simpleConfig
configDex := config.Get("services", "pydio.grpc.auth", "dex")
remarshall, _ := json.Marshal(configDex)
json.Unmarshal(remarshall, &dex)
var cIds []string
for _, c := range dex.StaticClients {
cIds = append(cIds, c.ID)
}
return &JWTVerifier{
IssuerUrl: dex.Issuer,
checkClientIds: cIds,
defaultClientID: dex.StaticClients[0].ID,
defaultClientSecret: dex.StaticClients[0].Secret,
}
}
type JWTVerifier struct {
IssuerUrl string
checkClientIds []string
defaultClientID string
defaultClientSecret string
}
// Verify validates an existing JWT token against the OIDC service that issued it
func (j *JWTVerifier) Verify(ctx context.Context, rawIDToken string) (context.Context, claim.Claims, error) {
claims := claim.Claims{}
ctx = oidc.ClientContext(ctx, &http.Client{
Transport: &http.Transport{
TLSClientConfig: config.GetTLSClientConfig("proxy"),
},
})
provider, err := oidc.NewProvider(ctx, j.IssuerUrl)
if err != nil {
log.Logger(ctx).Error("cannot init oidc provider", zap.Error(err))
return ctx, claims, err
}
var idToken *oidc.IDToken
var checkErr error
for _, clientId := range j.checkClientIds {
var verifier = provider.Verifier(&oidc.Config{ClientID: clientId, SkipNonceCheck: true})
// Parse and verify ID Token payload.
testToken, err := verifier.Verify(ctx, rawIDToken)
if err != nil {
log.Logger(ctx).Debug("jwt rawIdToken verify: failed", zap.Error(err))
checkErr = err
} else {
idToken = testToken
break
}
}
if idToken == nil {
return ctx, claims, checkErr
}
cli := auth.NewAuthTokenRevokerClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_AUTH, defaults.NewClient())
rsp, err := cli.MatchInvalid(ctx, &auth.MatchInvalidTokenRequest{
Token: rawIDToken,
})
if err != nil {
log.Logger(ctx).Error("verify", zap.Error(err))
return ctx, claims, err
}
if rsp.State == auth.State_REVOKED {
log.Logger(ctx).Error("jwt is verified but it is revoked")
return ctx, claim.Claims{}, errors.New("jwt was Revoked")
}
// Extract custom claims
if err := idToken.Claims(&claims); err != nil {
log.Logger(ctx).Error("cannot extract custom claims from idToken", zap.Error(err))
return ctx, claims, err
}
if claims.Name == "" {
log.Logger(ctx).Error("verify name")
return ctx, claims, errors.New("cannot find name inside claims")
}
ctx = context.WithValue(ctx, claim.ContextKey, claims)
md := make(map[string]string)
if existing, ok := metadata.FromContext(ctx); ok {
for k, v := range existing {
md[k] = v
}
}
md[common.PYDIO_CONTEXT_USER_KEY] = claims.Name
jsonClaims, _ := json.Marshal(claims)
md[claim.MetadataContextKey] = string(jsonClaims)
ctx = metadata.NewContext(ctx, md)
return ctx, claims, nil
}
// PasswordCredentialsToken will perform a call to the OIDC service with grantType "password"
// to get a valid token from a given user/pass credentials
func (j *JWTVerifier) PasswordCredentialsToken(ctx context.Context, userName string, password string) (context.Context, claim.Claims, error) {
// Get JWT From Dex
provider, _ := oidc.NewProvider(ctx, j.IssuerUrl)
// Configure an OpenID Connect aware OAuth2 client.
oauth2Config := oauth2.Config{
ClientID: j.defaultClientID,
ClientSecret: j.defaultClientSecret,
// Discovery returns the OAuth2 endpoints.
Endpoint: provider.Endpoint(),
// "openid" is a required scope for OpenID Connect flows.
Scopes: []string{oidc.ScopeOpenID, "profile", "email", "pydio"},
}
claims := claim.Claims{}
if token, err := oauth2Config.PasswordCredentialsToken(ctx, userName, password); err == nil {
idToken, _ := provider.Verifier(&oidc.Config{SkipClientIDCheck: true, SkipNonceCheck: true}).Verify(ctx, token.Extra("id_token").(string))
if e := idToken.Claims(&claims); e == nil {
if claims.Name == "" {
return ctx, claims, errors.New("No name inside Claims")
}
ctx = context.WithValue(ctx, claim.ContextKey, claims)
md := make(map[string]string)
if existing, ok := metadata.FromContext(ctx); ok {
for k, v := range existing {
md[k] = v
}
}
md[common.PYDIO_CONTEXT_USER_KEY] = claims.Name
ctx = metadata.NewContext(ctx, md)
return ctx, claims, nil
} else {
return ctx, claims, e
}
} else {
return ctx, claims, err
}
}
// Add a fake Claims in context to impersonate user
func WithImpersonate(ctx context.Context, user *idm.User) context.Context {
roles := make([]string, len(user.Roles))
for _, r := range user.Roles {
roles = append(roles, r.Uuid)
}
// Build Fake Claims Now
c := claim.Claims{
Name: user.Login,
Email: "TODO@pydio.com",
Roles: strings.Join(roles, ","),
}
return context.WithValue(ctx, claim.ContextKey, c)
}
// SubjectsForResourcePolicyQuery prepares a slice of strings that will be used to check for resource ownership.
// Can be extracted either from context or by loading a given user ID from database.
func SubjectsForResourcePolicyQuery(ctx context.Context, q *rest.ResourcePolicyQuery) (subjects []string, err error) {
if q == nil {
q = &rest.ResourcePolicyQuery{Type: rest.ResourcePolicyQuery_CONTEXT}
}
switch q.Type {
case rest.ResourcePolicyQuery_ANY, rest.ResourcePolicyQuery_NONE:
var value interface{}
if value = ctx.Value(claim.ContextKey); value == nil {
return subjects, errors2.BadRequest("resources", "Only admin profiles can list resources of other users")
}
claims := value.(claim.Claims)
if claims.Profile != common.PYDIO_PROFILE_ADMIN {
return subjects, errors2.Forbidden("resources", "Only admin profiles can list resources with ANY or NONE filter")
}
return subjects, nil
case rest.ResourcePolicyQuery_CONTEXT:
subjects = append(subjects, "*")
if value := ctx.Value(claim.ContextKey); value != nil {
claims := value.(claim.Claims)
subjects = append(subjects, "user:"+claims.Name)
// Add all profiles up to the current one (e.g admin will check for anon, shared, standard, admin)
for _, p := range common.PydioUserProfiles {
subjects = append(subjects, "profile:"+p)
if p == claims.Profile {
break
}
}
//subjects = append(subjects, "profile:"+claims.Profile)
for _, r := range strings.Split(claims.Roles, ",") {
subjects = append(subjects, "role:"+r)
}
} else {
log.Logger(ctx).Error("Cannot find claims in context", zap.Any("c", ctx))
subjects = append(subjects, "profile:anon")
}
case rest.ResourcePolicyQuery_USER:
if q.UserId == "" {
return subjects, errors2.BadRequest("resources", "Please provide a non-empty user id")
}
var value interface{}
if value = ctx.Value(claim.ContextKey); value == nil {
return subjects, errors2.BadRequest("resources", "Only admin profiles can list resources of other users")
}
claims := value.(claim.Claims)
if claims.Profile != common.PYDIO_PROFILE_ADMIN {
return subjects, errors2.Forbidden("resources", "Only admin profiles can list resources of other users")
}
subjects = append(subjects, "*")
subQ, _ := ptypes.MarshalAny(&idm.UserSingleQuery{
Uuid: q.UserId,
})
uClient := idm.NewUserServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_USER, defaults.NewClient())
if stream, e := uClient.SearchUser(ctx, &idm.SearchUserRequest{
Query: &service.Query{SubQueries: []*any.Any{subQ}},
}); e == nil {
var user *idm.User
for {
resp, err := stream.Recv()
if err != nil {
break
}
if resp == nil {
continue
}
user = resp.User
break
}
if user == nil {
return subjects, errors2.BadRequest("resources", "Cannot find user with id "+q.UserId)
}
for _, role := range user.Roles {
subjects = append(subjects, "role:"+role.Uuid)
}
subjects = append(subjects, "user:"+user.Login)
subjects = append(subjects, "profile:"+user.Attributes["profile"])
} else {
err = e
}
}
return
}