-
Notifications
You must be signed in to change notification settings - Fork 29
/
oauth2_token.go
74 lines (57 loc) · 2.22 KB
/
oauth2_token.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
package cmd
import (
"context"
"net/http"
"github.com/cloudentity/oauth2c/internal/oauth2"
)
func (c *OAuth2Cmd) ClientCredentialsGrantFlow(clientConfig oauth2.ClientConfig, serverConfig oauth2.ServerConfig, hc *http.Client) error {
return c.tokenEndpointFlow("Client Credentials Flow", clientConfig, serverConfig, hc)
}
func (c *OAuth2Cmd) PasswordGrantFlow(clientConfig oauth2.ClientConfig, serverConfig oauth2.ServerConfig, hc *http.Client) error {
return c.tokenEndpointFlow("Resource Owner Password Credentials Flow", clientConfig, serverConfig, hc)
}
func (c *OAuth2Cmd) RefreshTokenGrantFlow(clientConfig oauth2.ClientConfig, serverConfig oauth2.ServerConfig, hc *http.Client) error {
return c.tokenEndpointFlow("Refresh Token Flow", clientConfig, serverConfig, hc)
}
func (c *OAuth2Cmd) JWTBearerGrantFlow(clientConfig oauth2.ClientConfig, serverConfig oauth2.ServerConfig, hc *http.Client) error {
return c.tokenEndpointFlow("JWT Bearer Grant Flow", clientConfig, serverConfig, hc)
}
func (c *OAuth2Cmd) TokenExchangeGrantFlow(clientConfig oauth2.ClientConfig, serverConfig oauth2.ServerConfig, hc *http.Client) error {
return c.tokenEndpointFlow("Token Exchange Grant Flow", clientConfig, serverConfig, hc)
}
func (c *OAuth2Cmd) tokenEndpointFlow(
name string,
clientConfig oauth2.ClientConfig,
serverConfig oauth2.ServerConfig,
hc *http.Client,
requestTokenOpts ...oauth2.RequestTokenOption,
) error {
var (
tokenRequest oauth2.Request
tokenResponse oauth2.TokenResponse
err error
)
LogHeader(name)
// request token
LogSection("Request authorization")
authorizationStatus := LogAction("Requesting authorization")
if tokenRequest, tokenResponse, err = oauth2.RequestToken(
context.Background(),
clientConfig,
serverConfig,
hc,
requestTokenOpts...,
); err != nil {
LogRequestAndResponseln(tokenRequest, err)
return err
}
LogAssertion(tokenRequest, "Assertion", "assertion")
LogAssertion(tokenRequest, "Client assertion", "client_assertion")
LogSubjectTokenAndActorToken(tokenRequest)
LogAuthMethod(clientConfig)
LogRequestAndResponse(tokenRequest, tokenResponse)
LogTokenPayloadln(tokenResponse)
c.PrintResult(tokenResponse)
authorizationStatus("Authorization completed")
return nil
}