jwt: unify token generation functions
This commit is contained in:
parent
649c42f75b
commit
83273489e0
@ -544,7 +544,7 @@ func (h *ActionHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSource
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "failed to create git source")
|
return nil, errors.Wrapf(err, "failed to create git source")
|
||||||
}
|
}
|
||||||
token, err := common.GenerateJWTToken(h.sd, rs.Name, string(requestType), req)
|
token, err := common.GenerateOauth2JWTToken(h.sd, rs.Name, string(requestType), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -31,47 +31,39 @@ type TokenSigningData struct {
|
|||||||
Key []byte
|
Key []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateJWTToken(sd *TokenSigningData, remoteSourceName, requestType string, request interface{}) (string, error) {
|
func GenerateGenericJWTToken(sd *TokenSigningData, claims jwt.Claims) (string, error) {
|
||||||
|
token := jwt.NewWithClaims(sd.Method, claims)
|
||||||
|
|
||||||
|
var key interface{}
|
||||||
|
switch sd.Method {
|
||||||
|
case jwt.SigningMethodRS256:
|
||||||
|
key = sd.PrivateKey
|
||||||
|
case jwt.SigningMethodHS256:
|
||||||
|
key = sd.Key
|
||||||
|
default:
|
||||||
|
return "", errors.Errorf("unsupported signing method %q", sd.Method.Alg())
|
||||||
|
}
|
||||||
|
// Sign and get the complete encoded token as a string
|
||||||
|
return token.SignedString(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateOauth2JWTToken(sd *TokenSigningData, remoteSourceName, requestType string, request interface{}) (string, error) {
|
||||||
requestj, err := json.Marshal(request)
|
requestj, err := json.Marshal(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
token := jwt.NewWithClaims(sd.Method, jwt.MapClaims{
|
return GenerateGenericJWTToken(sd, jwt.MapClaims{
|
||||||
"exp": time.Now().Add(sd.Duration).Unix(),
|
"exp": time.Now().Add(sd.Duration).Unix(),
|
||||||
"remote_source_name": remoteSourceName,
|
"remote_source_name": remoteSourceName,
|
||||||
"request_type": requestType,
|
"request_type": requestType,
|
||||||
"request": string(requestj),
|
"request": string(requestj),
|
||||||
})
|
})
|
||||||
|
|
||||||
var key interface{}
|
|
||||||
switch sd.Method {
|
|
||||||
case jwt.SigningMethodRS256:
|
|
||||||
key = sd.PrivateKey
|
|
||||||
case jwt.SigningMethodHS256:
|
|
||||||
key = sd.Key
|
|
||||||
default:
|
|
||||||
errors.Errorf("unsupported signing method %q", sd.Method.Alg())
|
|
||||||
}
|
|
||||||
// Sign and get the complete encoded token as a string
|
|
||||||
return token.SignedString(key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateLoginJWTToken(sd *TokenSigningData, userID string) (string, error) {
|
func GenerateLoginJWTToken(sd *TokenSigningData, userID string) (string, error) {
|
||||||
token := jwt.NewWithClaims(sd.Method, jwt.MapClaims{
|
return GenerateGenericJWTToken(sd, jwt.MapClaims{
|
||||||
"sub": userID,
|
"sub": userID,
|
||||||
"exp": time.Now().Add(sd.Duration).Unix(),
|
"exp": time.Now().Add(sd.Duration).Unix(),
|
||||||
})
|
})
|
||||||
|
|
||||||
var key interface{}
|
|
||||||
switch sd.Method {
|
|
||||||
case jwt.SigningMethodRS256:
|
|
||||||
key = sd.PrivateKey
|
|
||||||
case jwt.SigningMethodHS256:
|
|
||||||
key = sd.Key
|
|
||||||
default:
|
|
||||||
errors.Errorf("unsupported signing method %q", sd.Method.Alg())
|
|
||||||
}
|
|
||||||
// Sign and get the complete encoded token as a string
|
|
||||||
return token.SignedString(key)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user