-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoauthflow.go
70 lines (59 loc) · 1.78 KB
/
oauthflow.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
package openapi
import (
"net/url"
"github.com/nasa9084/go-openapi/oauth"
)
// codebeat:disable[TOO_MANY_IVARS]
// OAuthFlow Object
type OAuthFlow struct {
flowType string
AuthorizationURL string `yaml:"authorizationUrl"`
TokenURL string `yaml:"tokenUrl"`
RefreshURL string `yaml:"refreshUrl"`
Scopes map[string]string
}
var defined = struct{}{}
var validFlowTypes = map[string]struct{}{
oauth.ImplicitFlow: defined,
oauth.PasswordFlow: defined,
oauth.ClientCredentialsFlow: defined,
oauth.AuthorizationCodeFlow: defined,
}
var requireAuthorizationURL = map[string]struct{}{
oauth.ImplicitFlow: defined,
oauth.AuthorizationCodeFlow: defined,
}
var requireTokenURL = map[string]struct{}{
oauth.PasswordFlow: defined,
oauth.ClientCredentialsFlow: defined,
oauth.AuthorizationCodeFlow: defined,
}
// SetFlowType sets oauth flow type.
func (oauthFlow *OAuthFlow) SetFlowType(typ string) {
oauthFlow.flowType = typ
}
// Validate the values of OAuthFlow object.
func (oauthFlow OAuthFlow) Validate() error {
if _, ok := validFlowTypes[oauthFlow.flowType]; !ok {
return ErrInvalidFlowType
}
if _, ok := requireAuthorizationURL[oauthFlow.flowType]; ok {
if err := mustURL("oauthFlow.authorizationUrl", oauthFlow.AuthorizationURL); err != nil {
return err
}
}
if _, ok := requireTokenURL[oauthFlow.flowType]; ok {
if err := mustURL("oauthFlow.tokenUrl", oauthFlow.TokenURL); err != nil {
return err
}
}
if oauthFlow.RefreshURL != "" {
if _, err := url.ParseRequestURI(oauthFlow.RefreshURL); err != nil {
return ErrFormatInvalid{Target: "oauthFlow.refreshUrl", Format: "URL"}
}
}
if oauthFlow.Scopes == nil || len(oauthFlow.Scopes) == 0 {
return ErrRequired{Target: "oauthFlow.scopes"}
}
return nil
}