-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.go
316 lines (279 loc) · 9.38 KB
/
validate.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
315
316
// Package oidc provides ...
package oidc
import (
"crypto/subtle"
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strings"
"github.com/deepzz0/oidc/protocol"
)
// URIValidationError error returned when validation don't match
type URIValidationError string
// Error implement error
func (e URIValidationError) Error() string {
return string(e)
}
func newURIValidationError(msg string, base string, redirect string) URIValidationError {
return URIValidationError(fmt.Sprintf("%s: %s / %s", msg, base, redirect))
}
// ParseMatchURL resolving uri references to base url
func ParseMatchURL(baseURL, redirectURL string) (retBaseURL, retRedirectURL *url.URL, err error) {
// parse base url
base, err := url.Parse(baseURL)
if err != nil {
return nil, nil, err
}
// parse redirect url
redirect, err := url.Parse(redirectURL)
if err != nil {
return nil, nil, err
}
// must not have fragment
if base.Fragment != "" || redirect.Fragment != "" {
return nil, nil, newURIValidationError("url must not include fragment.", baseURL, redirectURL)
}
// Scheme must match
if redirect.Scheme != base.Scheme {
return nil, nil, newURIValidationError("scheme mismatch", baseURL, redirectURL)
}
var (
redirectMatch bool
host string
)
// verify the hosts match
if redirect.Host == base.Host {
redirectMatch = true
host = base.Host
} else if baseIP := net.ParseIP(base.Host); baseIP != nil && baseIP.IsLoopback() && base.Scheme == "http" {
// if the registered redirect URI is lookback, then match the input redirect without the port.
if redirectIP := net.ParseIP(redirect.Hostname()); redirectIP != nil && redirectIP.IsLoopback() {
redirectMatch = true
host = redirect.Host
}
}
// Hosts must match
if !redirectMatch {
return nil, nil, newURIValidationError("host mismatch", baseURL, redirectURL)
}
// resolve references to base url
retBaseURL = (&url.URL{Scheme: base.Scheme, Host: host}).
ResolveReference(&url.URL{Path: base.Path})
retRedirectURL = (&url.URL{Scheme: base.Scheme, Host: host}).
ResolveReference(&url.URL{Path: redirect.Path, RawQuery: redirect.RawQuery})
return
}
// ValidateURI validates that redirectURI is contained in baseURI
func ValidateURI(baseURI, redirectURI string) (realRedirectURI string, err error) {
if baseURI == "" || redirectURI == "" {
return "", errors.New("urls cannot be blank")
}
base, redirect, err := ParseMatchURL(baseURI, redirectURI)
if err != nil {
return "", err
}
// allow exact path matched
if base.Path == redirect.Path {
return redirect.String(), nil
}
// ensure prefix matches are actually subpaths
requiredPrefix := strings.TrimRight(base.Path, "/") + "/"
if !strings.HasPrefix(redirect.Path, requiredPrefix) {
return "", newURIValidationError("path prefix doesn't match", baseURI, redirectURI)
}
return redirect.String(), nil
}
// ValidateURIList validates that redirectURI is contained in baseURIList.
func ValidateURIList(baseURIList, redirectURI, separator string) (realRedirectURI string, err error) {
uris := strings.Split(baseURIList, separator)
// if there are multiple client redirect uri's don't set the uri
if redirectURI == "" {
return "", newURIValidationError("redirect_uri not found", baseURIList, "")
}
// validates that redirectURI is contained in baseURIList.
for _, v := range uris {
realRedirectURI, err = ValidateURI(v, redirectURI)
// validated, return no error
if err == nil {
return realRedirectURI, nil
}
// if there was an error that is not a validation error, return it
if _, ok := err.(URIValidationError); !ok {
return "", err
}
}
return "", newURIValidationError("urls don't validate", baseURIList, redirectURI)
}
// ValidateScopes validates the scopes & remove invalid scope
func ValidateScopes(cli protocol.Client, scopes []string, defaultScopes []string,
respTypeCode bool, prompt []string) ([]string, bool, bool) {
openIDScope := false
offlineAccessScope := 0
for i := len(scopes) - 1; i >= 0; i-- {
scope := scopes[i]
if scope == protocol.ScopeOpenID {
openIDScope = true
continue
}
if scope == protocol.ScopeOfflineAccess {
offlineAccessScope = i
}
if !(scope == protocol.ScopeProfile || scope == protocol.ScopeEmail ||
scope == protocol.ScopeAddress || scope == protocol.ScopePhone ||
scope == protocol.ScopeOfflineAccess) && !cli.IsScopeAllowed(scope) { // ignore unknown scope
scopes[i] = scopes[len(scopes)-1]
scopes = scopes[:len(scopes)-1]
}
}
// MUST ignore the offline_access request unless the Client is using a response_type value that would
// result in an Authorization Code being returned,
// https://openid.net/specs/openid-connect-core-1_0.html#OfflineAccess
if offlineAccessScope > 0 {
found := false
for _, p := range prompt {
if protocol.Prompt(p) == protocol.PromptConsent {
found = true
}
}
// not oidc or not found consent prompt
if !openIDScope || !found {
offlineAccessScope = 0
scopes = append(scopes[:offlineAccessScope], scopes[offlineAccessScope+1:]...)
}
}
if len(scopes) == 0 {
scopes = defaultScopes
}
return scopes, openIDScope, offlineAccessScope > 0
}
func containsResponseType(types []protocol.ResponseType, ty string) bool {
for _, t := range types {
if string(t) == ty {
return true
}
}
return false
}
// ResponseTypeOK response type ok
type ResponseTypeOK struct {
ResponseTypeCode bool
ResponseTypeToken bool
ResponseTypeIDToken bool
ResponseTypeNone bool
ResponseTypeDevice bool
}
// ValidateResponseType validates the response type
func ValidateResponseType(cli protocol.Client, reqTypes []string) (ResponseTypeOK, error) {
ret := ResponseTypeOK{}
if len(reqTypes) == 0 {
return ret, protocol.ErrInvalidRequest.Desc("The response type is missing in your request. ")
}
types := cli.ResponseTypes()
for _, t := range reqTypes {
if !containsResponseType(types, t) {
return ret, protocol.ErrInvalidRequest.Desc("The requested response type is missing in the client configuration.")
}
switch protocol.ResponseType(t) {
case protocol.ResponseTypeCode:
ret.ResponseTypeCode = true
case protocol.ResponseTypeToken:
ret.ResponseTypeToken = true
case protocol.ResponseTypeIDToken:
ret.ResponseTypeIDToken = true
case protocol.ResponseTypeNone:
ret.ResponseTypeNone = true
case protocol.ResponseTypeDevice:
ret.ResponseTypeDevice = true
}
}
// The Response Type none SHOULD NOT be combined with other Response Types.
// https://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#none
if ret.ResponseTypeNone && len(reqTypes) > 1 {
return ret, protocol.ErrInvalidRequest.Desc("Response Type none SHOULD NOT be combined with other Response Types")
}
return ret, nil
}
// ValidateIDTokenHint validates the id_token_hint (if passed as parameter in the request)
// and returns the `sub` claim
func ValidateIDTokenHint(idTokenHit string) (string, error) {
if idTokenHit == "" {
return "", nil
}
// TODO
return "", nil
}
var pkceMatcher = regexp.MustCompile("^[a-zA-Z0-9~._-]{43,128}$")
// ValidateCodeChallenge validates the code challenge
// https://tools.ietf.org/html/rfc7636
func ValidateCodeChallenge(codeChall string, codeChallMethod protocol.CodeChallengeMethod) (protocol.CodeChallengeMethod, bool) {
if codeChall == "" {
return "", false
}
// allowed values are "plain" (default) and "S256",
// per https://tools.ietf.org/html/rfc7636#section-4.3
if codeChallMethod == "" {
codeChallMethod = protocol.CodeChallengeMethodPlain
}
if codeChallMethod != protocol.CodeChallengeMethodPlain &&
codeChallMethod != protocol.CodeChallengeMethodS256 {
return "", false
}
// https://tools.ietf.org/html/rfc7636#section-4.2
if pkceMatcher.MatchString(codeChall) {
return "", false
}
return codeChallMethod, true
}
// ValidateClientSecret determines whether the given secret matches a secret held by the client.
// Public clients return true for a secret of ""
func ValidateClientSecret(client protocol.Client, secret string) bool {
return subtle.ConstantTimeCompare([]byte(client.ClientSecret()), []byte(secret)) == 1
}
// ValidateGrantType validates the client grant type support
func ValidateGrantType(types []protocol.GrantType, ty protocol.GrantType) bool {
for _, v := range types {
if v == ty {
return true
}
}
return false
}
// ValidatePrompt validate prompt, set max_age=0 if prompt login is present
func ValidatePrompt(prompts []string, maxAge int) (int, error) {
for _, v := range prompts {
p := protocol.Prompt(v)
if !protocol.IsValidPrompt(p) {
return 0, errors.New("Unsupported prompt parameter: " + v)
}
// If this parameter contains none with any other value, an error is returned.
if p == protocol.PromptNone && len(prompts) > 1 {
return 0, errors.New("The prompt parameter 'none' must only be used as a signle value")
}
if p == protocol.PromptLogin {
maxAge = 0
}
}
return maxAge, nil
}
// ValidateTokenHint only support access_token & refresh_token
func ValidateTokenHint(hint protocol.TokenTypeHint) bool {
return hint == protocol.TokenTypeHintAccessToken ||
hint == protocol.TokenTypeHintRefreshToken
}
// ValidateOfflineAccess validate offline_access
func ValidateOfflineAccess(prompt []string, scopes []string) ([]string, bool, error) {
// ignoreScope := true
// for _, p := range prompt {
// if protocol.Prompt(p) == protocol.PromptConsent {
// for _, s := range scopes {
// if s == protocol.ScopeOfflineAccess {
// return true
// }
// }
// }
// }
//
return nil, false, nil
}