|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package jwt |
| 18 | + |
| 19 | +import ( |
| 20 | + "log" |
| 21 | + "net/http" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/golang-jwt/jwt/v5" |
| 25 | + "github.com/goplus/yap/ytest/auth" |
| 26 | +) |
| 27 | + |
| 28 | +// ----------------------------------------------------------------------------- |
| 29 | + |
| 30 | +type Signaturer struct { |
| 31 | + method jwt.SigningMethod |
| 32 | + claims jwt.MapClaims |
| 33 | + secret any |
| 34 | +} |
| 35 | + |
| 36 | +func (p *Signaturer) Set(k string, v any) *Signaturer { |
| 37 | + p.claims[k] = v |
| 38 | + return p |
| 39 | +} |
| 40 | + |
| 41 | +func (p *Signaturer) Audience(aud ...string) *Signaturer { |
| 42 | + p.claims["aud"] = jwt.ClaimStrings(aud) |
| 43 | + return p |
| 44 | +} |
| 45 | + |
| 46 | +func (p *Signaturer) Expiration(exp time.Time) *Signaturer { |
| 47 | + p.claims["exp"] = jwt.NewNumericDate(exp) |
| 48 | + return p |
| 49 | +} |
| 50 | + |
| 51 | +func (p *Signaturer) NotBefore(nbf time.Time) *Signaturer { |
| 52 | + p.claims["nbf"] = jwt.NewNumericDate(nbf) |
| 53 | + return p |
| 54 | +} |
| 55 | + |
| 56 | +func (p *Signaturer) IssuedAt(iat time.Time) *Signaturer { |
| 57 | + p.claims["iat"] = jwt.NewNumericDate(iat) |
| 58 | + return p |
| 59 | +} |
| 60 | + |
| 61 | +func (p *Signaturer) Compose(rt http.RoundTripper) http.RoundTripper { |
| 62 | + token := jwt.NewWithClaims(p.method, p.claims) |
| 63 | + raw, err := token.SignedString(p.secret) |
| 64 | + if err != nil { |
| 65 | + log.Panicln("jwt token.SignedString:", err) |
| 66 | + } |
| 67 | + return auth.WithToken(rt, "Bearer "+raw) |
| 68 | +} |
| 69 | + |
| 70 | +func newSign(method jwt.SigningMethod, secret any) *Signaturer { |
| 71 | + return &Signaturer{ |
| 72 | + method: method, |
| 73 | + claims: make(jwt.MapClaims), |
| 74 | + secret: secret, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// HS256 creates a signing methods by using the HMAC-SHA256. |
| 79 | +func HS256(key []byte) *Signaturer { |
| 80 | + return newSign(jwt.SigningMethodHS256, key) |
| 81 | +} |
| 82 | + |
| 83 | +// HS384 creates a signing methods by using the HMAC-SHA384. |
| 84 | +func HS384(key []byte) *Signaturer { |
| 85 | + return newSign(jwt.SigningMethodHS384, key) |
| 86 | +} |
| 87 | + |
| 88 | +// HS512 creates a signing methods by using the HMAC-SHA512. |
| 89 | +func HS512(key []byte) *Signaturer { |
| 90 | + return newSign(jwt.SigningMethodHS512, key) |
| 91 | +} |
| 92 | + |
| 93 | +// ----------------------------------------------------------------------------- |
0 commit comments