Skip to content

Commit 134c330

Browse files
committed
🩹 Check aud in ID token manually
1 parent bce813b commit 134c330

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

internal/traq/oidc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ func generateAuthProofs() (*service.AuthProofs, error) {
4444
}, nil
4545
}
4646

47+
func (c *Client) checkIDTokenAudience(idToken *oidc.IDToken) error {
48+
for _, aud := range idToken.Audience {
49+
if aud == c.clientID {
50+
return nil
51+
}
52+
}
53+
return service.NewUnauthenticatedError("invalid id token: audience mismatch")
54+
}
55+
4756
func (c *Client) NewAuthCodeURL(ctx context.Context) (*url.URL, *service.AuthProofs, error) {
4857
authProofs, err := generateAuthProofs()
4958
if err != nil {
@@ -90,6 +99,9 @@ func (c *Client) ExchangeCodeToToken(
9099
return "", service.NewUnauthenticatedError("invalid authorization code").
91100
WithInternal(err)
92101
}
102+
if err := c.checkIDTokenAudience(idToken); err != nil {
103+
return "", err
104+
}
93105
return service.Token(rawIDToken), nil
94106
}
95107

internal/traq/traq.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
type Client struct {
13+
clientID string
1314
oauth2Config *oauth2.Config
1415
idTokenVerifier *oidc.IDTokenVerifier
1516
}
@@ -37,10 +38,15 @@ func LoadClient(ctx context.Context, config ClientConfig) (*Client, error) {
3738
idTokenVerifier := provider.Verifier(&oidc.Config{
3839
ClientID: config.ClientID,
3940
// 他のアプリが発行したトークンも受け入れるため、クライアントIDのチェックはスキップする
41+
// 必要に応じてaudのチェックを入れる; checkIDTokenAudienceを参照
4042
SkipClientIDCheck: true,
4143
SkipExpiryCheck: false,
4244
SkipIssuerCheck: false,
4345
InsecureSkipSignatureCheck: false,
4446
})
45-
return &Client{oauth2Config: oauth2Config, idTokenVerifier: idTokenVerifier}, nil
47+
return &Client{
48+
clientID: config.ClientID,
49+
oauth2Config: oauth2Config,
50+
idTokenVerifier: idTokenVerifier,
51+
}, nil
4652
}

0 commit comments

Comments
 (0)