-
Notifications
You must be signed in to change notification settings - Fork 3
/
okta.go
162 lines (139 loc) · 3.92 KB
/
okta.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
package device
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/oauth2"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
)
type Okta struct{}
func (o Okta) Config(org, clientID string) *Config {
return &Config{
OAuth2Config: oauth2.Config{
ClientID: clientID,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("https://%s.okta.com/oauth2/v1/device/authorize", org),
TokenURL: fmt.Sprintf("https://%s.okta.com/oauth2/v1/token", org),
},
Scopes: DefaultScopes,
},
Audience: fmt.Sprintf("https://%s.okta.com", org),
Issuer: fmt.Sprintf("https://%s.okta.com", org),
KeyURI: fmt.Sprintf("https://%s.okta.com/oauth2/v1/keys", org),
client: &http.Client{},
org: org,
}
}
func NewOktaOnlineValidator(cfg *Config) *OktaOnlineValidator {
return &OktaOnlineValidator{
Config: cfg,
introspectionURI: fmt.Sprintf("https://%s.okta.com/oauth2/v1/introspect", cfg.org),
}
}
type OktaOnlineValidator struct {
*Config
introspectionURI string
}
func (o *OktaOnlineValidator) Initialize(_ context.Context) error {
return nil
}
func (o *OktaOnlineValidator) Validate(ctx context.Context, tokenString string) error {
oir, err := o.Introspect(ctx, tokenString)
if err != nil {
return err
}
return oir.Valid(o.Config)
}
// Introspect calls the Okta OAuth2 API to validate the token, see
// https://developer.okta.com/docs/reference/api/oidc/#introspect
func (o *OktaOnlineValidator) Introspect(ctx context.Context, tokenString string) (OktaIntrospectionResponse, error) {
// POST https://rockset.okta.com/oauth2/v1/introspect?client_id=${CLIENT_ID}&token=${TOKEN}
var oir OktaIntrospectionResponse
u, err := url.Parse(o.introspectionURI)
if err != nil {
return oir, err
}
values := u.Query()
values.Set("client_id", o.OAuth2Config.ClientID)
values.Set("token", tokenString)
u.RawQuery = values.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), strings.NewReader(""))
if err != nil {
return oir, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
response, err := o.client.Do(req)
if err != nil {
return oir, err
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Printf("error closing response body: %v", err)
}
}()
if response.StatusCode != http.StatusOK {
status := response.Status
body, err := io.ReadAll(response.Body)
if err == nil {
status = string(body)
}
return oir, fmt.Errorf("unexpected return code (%d): %s", response.StatusCode, status)
}
d := json.NewDecoder(response.Body)
if err = d.Decode(&oir); err != nil {
return oir, err
}
return oir, nil
}
// {
// Active:true
// Scope:openid profile offline_access groups
// Username:[email protected]
// Exp:1690302787
// Iat:1690299187
// Sub:[email protected]
// Aud:https://rockset.okta.com
// Iss:https://rockset.okta.com
// Jti:AT.5mjpDSDqVSGQAgSwsEUs6ZGRWq0q2qn-sxHqMURStCw
// TokenType:Bearer
// ClientId:0oa6zy1dgtX2nLzKv5d7
// Uid:00usmr7rbxb5p5b8P5d6
// }
type OktaIntrospectionResponse struct {
Active bool `json:"active"`
Scope string `json:"scope"`
Username string `json:"username"`
Exp int64 `json:"exp"`
Nbf int64 `json:"nbf"`
Iat int64 `json:"iat"`
Sub string `json:"sub"`
Aud string `json:"aud"`
Iss string `json:"iss"`
Jti string `json:"jti"`
TokenType string `json:"token_type"`
ClientId string `json:"client_id"`
DeviceId string `json:"device_id"`
Uid string `json:"uid"`
}
func (i OktaIntrospectionResponse) Valid(cfg *Config) error {
if !i.Active {
return fmt.Errorf("inactive token")
}
if i.ClientId != cfg.OAuth2Config.ClientID {
return fmt.Errorf("incorrect issuer: %s", i.Iss)
}
if i.Aud != cfg.Audience {
return fmt.Errorf("incorrect audience: %s", i.Aud)
}
t := time.Unix(i.Exp, 0)
if time.Now().After(t) {
return fmt.Errorf("expired: %s", t.String())
}
return nil
}