-
Notifications
You must be signed in to change notification settings - Fork 14
/
oauth2.go
152 lines (128 loc) · 4.12 KB
/
oauth2.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
package lib
import (
"fmt"
"net/url"
"github.com/hashicorp/hcl/v2"
pkce "github.com/jimlambrt/go-oauth-pkce-code-verifier"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/avenga/couper/config"
)
const (
CodeVerifier = "code_verifier"
FnOAuthAuthorizationURL = "oauth2_authorization_url"
FnOAuthVerifier = "oauth2_verifier"
InternalFnOAuthHashedVerifier = "internal_oauth_hashed_verifier"
)
var NoOpOAuthAuthorizationURLFunction = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "oauth2_label",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
if len(args) > 0 {
return cty.StringVal(""), fmt.Errorf("missing oidc or beta_oauth2 block with referenced label %q", args[0].AsString())
}
return cty.StringVal(""), fmt.Errorf("missing oidc or beta_oauth2 definitions")
},
})
func NewOAuthAuthorizationURLFunction(ctx *hcl.EvalContext, oauth2s map[string]config.OAuth2Authorization,
verifier func() (*pkce.CodeVerifier, error), origin *url.URL,
evalFn func(*hcl.EvalContext, hcl.Expression) (cty.Value, error)) function.Function {
emptyStringVal := cty.StringVal("")
return function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "oauth2_label",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (cty.Value, error) {
label := args[0].AsString()
oauth2, exist := oauth2s[label]
if !exist {
return NoOpOAuthAuthorizationURLFunction.Call(args)
}
authorizationEndpoint, err := oauth2.GetAuthorizationEndpoint()
if err != nil {
return emptyStringVal, err
}
oauthAuthorizationURL, err := url.Parse(authorizationEndpoint)
if err != nil {
return emptyStringVal, err
}
redirectURI := oauth2.GetRedirectURI()
if redirectURI == "" {
return emptyStringVal, fmt.Errorf("redirect_uri is required")
}
absRedirectURI, err := AbsoluteURL(redirectURI, origin)
if err != nil {
return emptyStringVal, err
}
query := oauthAuthorizationURL.Query()
query.Set("response_type", "code")
query.Set("client_id", oauth2.GetClientID())
query.Set("redirect_uri", absRedirectURI)
if scope := oauth2.GetScope(); scope != "" {
query.Set("scope", scope)
}
verifierMethod, err := oauth2.GetVerifierMethod()
if err != nil {
return cty.StringVal(""), err
}
if verifierMethod == config.CcmS256 {
codeChallenge, err := createCodeChallenge(verifier)
if err != nil {
return cty.StringVal(""), err
}
query.Set("code_challenge_method", "S256")
query.Set("code_challenge", codeChallenge)
} else {
hashedVerifier, err := createCodeChallenge(verifier)
if err != nil {
return cty.StringVal(""), err
}
query.Set(verifierMethod, hashedVerifier)
}
oauthAuthorizationURL.RawQuery = query.Encode()
return cty.StringVal(oauthAuthorizationURL.String()), nil
},
})
}
func NewOAuthCodeVerifierFunction(verifier func() (*pkce.CodeVerifier, error)) function.Function {
return function.New(&function.Spec{
Params: []function.Parameter{},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
codeVerifier, err := verifier()
if err != nil {
return cty.StringVal(""), err
}
return cty.StringVal(codeVerifier.String()), nil
},
})
}
func NewOAuthCodeChallengeFunction(verifier func() (*pkce.CodeVerifier, error)) function.Function {
return function.New(&function.Spec{
Params: []function.Parameter{},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
codeChallenge, err := createCodeChallenge(verifier)
if err != nil {
return cty.StringVal(""), err
}
return cty.StringVal(codeChallenge), nil
},
})
}
func createCodeChallenge(verifier func() (*pkce.CodeVerifier, error)) (string, error) {
codeVerifier, err := verifier()
if err != nil {
return "", err
}
return codeVerifier.CodeChallengeS256(), nil
}