/
sastpassclient.go
183 lines (162 loc) · 5.08 KB
/
sastpassclient.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package CxSASTClientGo
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
func OauthCodeCallbackURL(base_url, client_id, redirect_uri, scope, state string) string {
return fmt.Sprintf("%v/cxrestapi/auth/identity/connect/authorize?client_id=%v&redirect_uri=%v&response_type=code&scope=%v&state=%v", base_url, client_id, redirect_uri, scope, state)
}
func OauthCodeHTTPClient(client *http.Client, base_url, client_id, oauth_code, oauth_scope, oauth_redirect_uri string) (*http.Client, error) {
// should auto-refresh afterwards
//fmt.Println("SASTRefreshClient - GetAccessToken")
ctx := context.Background()
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
params := url.Values{
"client_id": {client_id},
"code": {oauth_code},
"grant_type": {"authorization_code"},
"redirect_uri": {oauth_redirect_uri},
}
resp, err := client.Post(fmt.Sprintf("%v/cxrestapi/auth/identity/connect/token", base_url), "application/x-www-form-urlencoded", strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
returnval := map[string]interface{}{}
err = json.Unmarshal(body, &returnval)
if err != nil {
return nil, err
}
if returnval["access_token"] == nil {
return nil, errors.New(fmt.Sprintf("no access_token returned in response: %v", string(body)))
}
if returnval["refresh_token"] == nil {
return nil, errors.New(fmt.Sprintf("no refresh_token returned in response: %v", string(body)))
}
conf := &oauth2.Config{
ClientID: client_id,
Endpoint: oauth2.Endpoint{
TokenURL: fmt.Sprintf("%v/cxrestapi/auth/identity/connect/token", base_url),
},
}
refreshToken := &oauth2.Token{
AccessToken: returnval["access_token"].(string),
RefreshToken: returnval["refresh_token"].(string),
Expiry: time.Now().UTC().Add(time.Duration(returnval["expires_in"].(float64)) * time.Second),
}
//fmt.Printf("refresh token: %v\n", refreshToken.RefreshToken)
return conf.Client(ctx, refreshToken), nil
}
func OauthCredentialClient(client *http.Client, base_url, client_id, client_secret, username, password string, scopes []string) *http.Client {
ctx := context.Background()
// client.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
conf := &PasswordConfig{
Config: oauth2.Config{
ClientID: client_id,
ClientSecret: client_secret,
Scopes: scopes,
Endpoint: oauth2.Endpoint{
TokenURL: fmt.Sprintf("%v/cxrestapi/auth/identity/connect/token", base_url),
},
},
Username: username,
Password: password,
}
return conf.Client(ctx)
}
type PasswordConfig struct {
Config oauth2.Config
Username string
Password string
ReuseSource *PasswordTokenSource
}
func (c *PasswordConfig) Token(ctx context.Context) (*oauth2.Token, error) {
// fmt.Println("PasswordConfig.Token")
return c.TokenSource(ctx).Token()
}
func (c *PasswordConfig) Client(ctx context.Context) *http.Client {
// fmt.Println("PasswordConfig.Client")
return oauth2.NewClient(ctx, c.TokenSource(ctx))
}
func (c *PasswordConfig) TokenSource(ctx context.Context) oauth2.TokenSource {
// fmt.Println("PasswordConfig.TokenSource")
if c.ReuseSource == nil {
c.ReuseSource = &PasswordTokenSource{
ctx: ctx,
conf: c,
}
}
return c.ReuseSource
}
type PasswordTokenSource struct {
ctx context.Context
conf *PasswordConfig
LastToken *oauth2.Token
}
func (c *PasswordTokenSource) Token() (*oauth2.Token, error) {
// fmt.Println("PasswordTokenSource.Token")
if c.LastToken.Valid() {
// fmt.Println("Last token is still valid")
return c.LastToken, nil
}
var err error
c.LastToken, err = c.conf.Config.PasswordCredentialsToken(c.ctx, c.conf.Username, c.conf.Password)
return c.LastToken, err
}
/*
OIDC client for refresh tokens, set up on SAST:
{
"updateAccessTokenClaimsOnRefresh": true,
"accessTokenType": 0,
"includeJwtId": true,
"alwaysIncludeUserClaimsInIdToken": true,
"clientId": "custom_client",
"clientName": "custom_client",
"allowOfflineAccess": true,
"clientSecrets": [],
"allowedGrantTypes": [
"offline_access",
"authorization_code"
],
"allowedScopes": [
"sast_api",
"access_control_api"
],
"enabled": true,
"requireClientSecret": false,
"redirectUris": [
"http://custom_client:8000/oauth_redirect.html",
"http://custom_client/oauth_redirect.html"
],
"postLogoutRedirectUris": [],
"frontChannelLogoutUri": null,
"frontChannelLogoutSessionRequired": false,
"backChannelLogoutUri": null,
"backChannelLogoutSessionRequired": false,
"identityTokenLifetime": 3660,
"accessTokenLifetime": 3660,
"authorizationCodeLifetime": 3660,
"absoluteRefreshTokenLifetime": 3660,
"slidingRefreshTokenLifetime": 3660,
"refreshTokenUsage": 3660,
"refreshTokenExpiration": 3660,
"allowedCorsOrigins": [],
"allowAccessTokensViaBrowser": true,
"claims": [],
"clientClaimsPrefix": null,
"requirePkce": false
}
*/