Skip to content

Commit 1079448

Browse files
committed
feat: support ecdsa key/algo
1 parent b415b31 commit 1079448

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

goic.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goic
22

33
import (
4+
"crypto/ecdsa"
45
"crypto/rsa"
56
"encoding/json"
67
"errors"
@@ -245,25 +246,24 @@ func (g *Goic) verifyToken(p *Provider, tok *Token, nonce string) error {
245246
return err
246247
}
247248

248-
// todo: is this ok here?
249-
if p.wellKnown.KeysURI == "" {
250-
return nil
251-
}
252-
253249
_, err = jwt.ParseWithClaims(tok.IDToken, claims, func(t *jwt.Token) (interface{}, error) {
254250
alg := t.Header["alg"].(string)
255-
if alg == "HS256" || alg == "HS384" || alg == "HS512" {
251+
al2 := alg[0:2]
252+
if al2 == "HS" {
256253
return []byte(p.clientSecret), nil
257254
}
258-
259-
if alg != "RS256" && alg != "RS384" && alg != "RS512" {
255+
if al2 != "RS" && al2 != "ES" {
260256
return nil, ErrTokenAlgo
261257
}
262258

263259
for _, key := range p.wellKnown.jwks.Keys {
264-
if (key.Kty == "RSA" && key.Kid == t.Header["kid"]) || (key.Alg == alg && key.Kid == t.Header["kid"]) {
260+
kid := key.Kid == t.Header["kid"]
261+
if kid && key.Kty == "RSA" && key.Alg == alg {
265262
return &rsa.PublicKey{E: ParseExponent(key.E), N: ParseModulo(key.N)}, nil
266263
}
264+
if kid && key.Kty == "EC" && key.Alg == alg {
265+
return &ecdsa.PublicKey{X: ParseModulo(key.X), Y: ParseModulo(key.Y), Curve: GetCurve(key.Crv)}, nil
266+
}
267267
}
268268

269269
return nil, ErrTokenKey

provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ type WellKnown struct {
2929
jwks struct {
3030
Keys []struct {
3131
Alg string `json:"alg"`
32-
Use string `json:"use"`
32+
Use string `json:"use,omitempty"`
3333
Kid string `json:"kid"`
3434
Kty string `json:"kty"`
35-
E string `json:"e"`
36-
N string `json:"n"`
35+
Crv string `json:"crv,omitempty"`
36+
E string `json:"e,omitempty"`
37+
N string `json:"n,omitempty"`
38+
X string `json:"x,omitempty"`
39+
Y string `json:"y,omitempty"`
3740
}
3841
}
3942
}

0 commit comments

Comments
 (0)