-
Notifications
You must be signed in to change notification settings - Fork 14
/
auth_code_client.go
62 lines (51 loc) · 1.88 KB
/
auth_code_client.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
package oauth2
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/hashicorp/hcl/v2"
"github.com/avenga/couper/config"
)
// AuthCodeFlowClient represents an OAuth2 client using the authorization code flow.
type AuthCodeFlowClient interface {
ExchangeCodeAndGetTokenResponse(req *http.Request, callbackURL *url.URL) (map[string]interface{}, error)
validateTokenResponseData(ctx context.Context, tokenResponseData map[string]interface{}, hashedVerifierValue, verifierValue, accessToken string) error
}
// AuthCodeClient represents an OAuth2 client using the (plain) authorization code flow.
type AuthCodeClient struct {
*AbstractAuthCodeClient
}
// NewAuthCodeClient creates a new OAuth2 Authorization Code client.
func NewAuthCodeClient(evalCtx *hcl.EvalContext, acClientConf config.OAuth2AcClient, oauth2AsConf config.OAuth2AS, backend http.RoundTripper) (*AuthCodeClient, error) {
grantType := acClientConf.GetGrantType()
if grantType != "authorization_code" {
return nil, fmt.Errorf("grant_type %s not supported", grantType)
}
switch acClientConf.(type) {
case *config.OAuth2AC:
verifierMethod, err := acClientConf.GetVerifierMethod()
if err != nil {
return nil, err
}
if verifierMethod != config.CcmS256 && verifierMethod != "state" {
return nil, fmt.Errorf("verifier_method %s not supported", verifierMethod)
}
default:
// skip this for oidc configurations due to possible startup errors
}
client, err := NewClient(evalCtx, grantType, oauth2AsConf, acClientConf, backend)
if err != nil {
return nil, err
}
o := &AuthCodeClient{&AbstractAuthCodeClient{
acClientConf: acClientConf,
Client: client,
}}
o.AuthCodeFlowClient = o
return o, nil
}
// validateTokenResponseData validates the token response data (no-op)
func (o *AuthCodeClient) validateTokenResponseData(_ context.Context, _ map[string]interface{}, _, _, _ string) error {
return nil
}