-
Notifications
You must be signed in to change notification settings - Fork 15
/
oauth2.go
49 lines (40 loc) · 1.28 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
package accesscontrol
import (
"context"
"net/http"
"github.com/avenga/couper/config/request"
"github.com/avenga/couper/errors"
"github.com/avenga/couper/oauth2"
)
var _ AccessControl = &OAuth2Callback{}
// OAuth2Callback represents the access control for the OAuth2 authorization code flow callback.
type OAuth2Callback struct {
oauth2Client oauth2.AuthCodeFlowClient
name string
}
// NewOAuth2Callback creates a new access control for the OAuth2 authorization code flow callback.
func NewOAuth2Callback(oauth2Client oauth2.AuthCodeFlowClient, name string) *OAuth2Callback {
return &OAuth2Callback{
oauth2Client: oauth2Client,
name: name,
}
}
// Validate implements the AccessControl interface
func (oa *OAuth2Callback) Validate(req *http.Request) error {
if req.Method != http.MethodGet {
return errors.Oauth2.Messagef("wrong method (%s)", req.Method)
}
tokenResponseData, err := oa.oauth2Client.ExchangeCodeAndGetTokenResponse(req, req.URL)
if err != nil {
return err
}
ctx := req.Context()
acMap, ok := ctx.Value(request.AccessControls).(map[string]interface{})
if !ok {
acMap = make(map[string]interface{})
}
acMap[oa.name] = tokenResponseData
ctx = context.WithValue(ctx, request.AccessControls, acMap)
*req = *req.WithContext(ctx)
return nil
}