Commit 0b19761fb3f33500435a3469a438294f80ee01a8

Parents: 635341b4e223e8b14076e310e42bcffe9d08b486

From: Moritz Poldrack <git@moritz.sh>
Date: Thu Jan 11 10:36:16 2024 +0700

add ability to force updates of token claims

		

Stats

token-fields.go +144/-0
token.go +7/-76

Changeset

  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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
diff --git a/token-fields.go b/token-fields.go
new file mode 100644
index 0000000000000000000000000000000000000000..30288cca8037f21f5bb8fcedeee548c86016a561
--- /dev/null
+++ b/token-fields.go
@@ -0,0 +1,144 @@
+package oidc
+
+// SPDX-FileCopyrightText: © Moritz Poldrack
+// SPDX-License-Identifier: MPL-2.0
+
+import (
+	"context"
+	"fmt"
+	"time"
+
+	"github.com/lestrrat-go/jwx/v2/jwt"
+	"github.com/lestrrat-go/jwx/v2/jwt/openid"
+)
+
+// UpdateClaims forces an update of the information in a token. By default, the
+// results are cached and if a user was to change any field in the identity
+// provider, they have to login again for the token to reflect that.
+func (t *Token) UpdateClaims() error {
+	token, err := t.source.Token()
+	if err != nil {
+		return fmt.Errorf("failed to acquire token: %w", err)
+	}
+
+	keyset, err := t.cfg.jwtKeyCache.Get(context.Background(), t.cfg.cfg.JwksURI)
+	if err != nil {
+		return fmt.Errorf("failed to retrieve JWT keys: %w", err)
+	}
+	idToken, err := jwt.Parse(
+		[]byte(fmt.Sprint(token.Extra("id_token"))),
+		jwt.WithToken(openid.New()),
+		jwt.WithVerify(!t.cfg.options.has(OptionSkipTokenValidation)),
+		jwt.WithKeySet(keyset),
+	)
+	if err == nil {
+		return fmt.Errorf("failed to parse JWT: %w", err)
+	}
+
+	oidToken, ok := idToken.(openid.Token)
+	if !ok {
+		return fmt.Errorf("provided token is not an OIDC token")
+	}
+
+	t.idToken = oidToken
+	return nil
+}
+
+func (t *Token) Email() string {
+	return t.idToken.Email()
+}
+
+func (t *Token) Address() *openid.AddressClaim {
+	return t.idToken.Address()
+}
+
+func (t *Token) Birthdate() time.Time {
+	bd := t.idToken.Birthdate()
+	timeDay, _ := time.Parse("20060102", fmt.Sprintf("%d%02d%02d", bd.Year(), bd.Month(), bd.Day()))
+	return timeDay
+}
+
+func (t *Token) EmailVerified() bool {
+	return t.idToken.EmailVerified()
+}
+
+func (t *Token) FamilyName() string {
+	return t.idToken.FamilyName()
+}
+
+func (t *Token) Gender() string {
+	return t.idToken.Gender()
+}
+
+func (t *Token) GivenName() string {
+	return t.idToken.GivenName()
+}
+
+func (t *Token) Locale() string {
+	return t.idToken.Locale()
+}
+
+func (t *Token) MiddleName() string {
+	return t.idToken.MiddleName()
+}
+
+func (t *Token) Name() string {
+	return t.idToken.Name()
+}
+
+func (t *Token) Nickname() string {
+	return t.idToken.Nickname()
+}
+
+func (t *Token) PhoneNumber() string {
+	return t.idToken.PhoneNumber()
+}
+
+func (t *Token) PhoneNumberVerified() bool {
+	return t.idToken.PhoneNumberVerified()
+}
+
+func (t *Token) Picture() string {
+	return t.idToken.Picture()
+}
+
+func (t *Token) PreferredUsername() string {
+	return t.idToken.PreferredUsername()
+}
+
+func (t *Token) Profile() string {
+	return t.idToken.Profile()
+}
+
+func (t *Token) UpdatedAt() time.Time {
+	return t.idToken.UpdatedAt()
+}
+
+func (t *Token) Website() string {
+	return t.idToken.Website()
+}
+
+func (t *Token) Zoneinfo() string {
+	return t.idToken.Zoneinfo()
+}
+
+func (t *Token) Subject() string {
+	return t.idToken.Subject()
+}
+
+func (t *Token) Audience() []string {
+	return t.idToken.Audience()
+}
+
+func (t *Token) Expiration() time.Time {
+	tok, err := t.Token()
+	if err != nil {
+		return time.Unix(0, 0)
+	}
+	return tok.Expiry
+}
+
+// GetField returns the specified field and whether or not it is set.
+func (t *Token) GetField(field string) (any, bool) {
+	return t.idToken.Get(field)
+}
diff --git a/token.go b/token.go
index 2d36c5008699465148c49bf87b12aeb670a8afcf..130b9757192eef38a05e2a94b134d46e14672618 100644
--- a/token.go
+++ b/token.go
@@ -45,11 +45,13 @@ // Token returns an OIDC token returned by the service
 type Token struct {
 	source  oauth2.TokenSource
 	idToken openid.Token
+	cfg     *Configuration
 }
 
 func (cfg *Configuration) newToken(tok *oauth2.Token) *Token {
 	result := &Token{
 		source: cfg.oauthCfg.TokenSource(context.Background(), tok),
+		cfg:    cfg,
 	}
 
 	token := fmt.Sprint(tok.Extra("id_token"))
@@ -110,84 +112,13 @@
 	return result, nil
 }
 
+// Token returns the current OAuth2 token associated with the OIDC token. You
+// probably don't want this.
 func (t *Token) Token() (*oauth2.Token, error) {
 	return t.source.Token()
 }
 
-func (t *Token) Email() string {
-	return t.idToken.Email()
-}
-
-func (t *Token) Address() *openid.AddressClaim {
-	return t.idToken.Address()
-}
-
-func (t *Token) Birthdate() time.Time {
-	bd := t.idToken.Birthdate()
-	timeDay, _ := time.Parse("20060102", fmt.Sprintf("%d%02d%02d", bd.Year(), bd.Month(), bd.Day()))
-	return timeDay
-}
-
-func (t *Token) EmailVerified() bool {
-	return t.idToken.EmailVerified()
-}
-
-func (t *Token) FamilyName() string {
-	return t.idToken.FamilyName()
-}
-
-func (t *Token) Gender() string {
-	return t.idToken.Gender()
-}
-
-func (t *Token) GivenName() string {
-	return t.idToken.GivenName()
-}
-
-func (t *Token) Locale() string {
-	return t.idToken.Locale()
-}
-
-func (t *Token) MiddleName() string {
-	return t.idToken.MiddleName()
-}
-
-func (t *Token) Name() string {
-	return t.idToken.Name()
-}
-
-func (t *Token) Nickname() string {
-	return t.idToken.Nickname()
-}
-
-func (t *Token) PhoneNumber() string {
-	return t.idToken.PhoneNumber()
-}
-
-func (t *Token) PhoneNumberVerified() bool {
-	return t.idToken.PhoneNumberVerified()
-}
-
-func (t *Token) Picture() string {
-	return t.idToken.Picture()
-}
-
-func (t *Token) PreferredUsername() string {
-	return t.idToken.PreferredUsername()
-}
-
-func (t *Token) Profile() string {
-	return t.idToken.Profile()
-}
-
-func (t *Token) UpdatedAt() time.Time {
-	return t.idToken.UpdatedAt()
-}
-
-func (t *Token) Website() string {
-	return t.idToken.Website()
-}
-
-func (t *Token) Zoneinfo() string {
-	return t.idToken.Zoneinfo()
+func (t *Token) Valid() bool {
+	exp := t.Expiration()
+	return exp.After(time.Now())
 }