55 "strings"
66 "time"
77
8+ errs "errors"
89 "github.com/dgrijalva/jwt-go"
910 "gopkg.in/oauth2.v3"
1011 "gopkg.in/oauth2.v3/errors"
@@ -49,7 +50,23 @@ func (a *JWTAccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool)
4950 }
5051
5152 token := jwt .NewWithClaims (a .SignedMethod , claims )
52- access , err = token .SignedString (a .SignedKey )
53+ var key interface {}
54+ if a .isEs () {
55+ key , err = jwt .ParseECPrivateKeyFromPEM (a .SignedKey )
56+ if err != nil {
57+ return "" , "" , err
58+ }
59+ } else if a .isRsOrPS () {
60+ key , err = jwt .ParseRSAPrivateKeyFromPEM (a .SignedKey )
61+ if err != nil {
62+ return "" , "" , err
63+ }
64+ } else if a .isHs () {
65+ key = a .SignedKey
66+ } else {
67+ return "" , "" , errs .New ("unsupported sign method" )
68+ }
69+ access , err = token .SignedString (key )
5370 if err != nil {
5471 return
5572 }
@@ -61,3 +78,17 @@ func (a *JWTAccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool)
6178
6279 return
6380}
81+
82+ func (a * JWTAccessGenerate ) isEs () bool {
83+ return strings .HasPrefix (a .SignedMethod .Alg (), "ES" )
84+ }
85+
86+ func (a * JWTAccessGenerate ) isRsOrPS () bool {
87+ isRs := strings .HasPrefix (a .SignedMethod .Alg (), "RS" )
88+ isPs := strings .HasPrefix (a .SignedMethod .Alg (), "PS" )
89+ return isRs || isPs
90+ }
91+
92+ func (a * JWTAccessGenerate ) isHs () bool {
93+ return strings .HasPrefix (a .SignedMethod .Alg (), "HS" )
94+ }
0 commit comments