2023-06-18 13:03:41 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2023-10-29 12:28:21 +00:00
|
|
|
"github.com/1f349/mjwt"
|
|
|
|
"github.com/1f349/mjwt/claims"
|
2023-06-20 00:32:16 +01:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2023-06-18 13:03:41 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateTokenPair creates an access and refresh token pair using the default
|
|
|
|
// 15 minute and 7 day durations respectively
|
2023-06-20 00:32:16 +01:00
|
|
|
func CreateTokenPair(p mjwt.Signer, sub, id, rId string, aud, rAud jwt.ClaimStrings, perms *claims.PermStorage) (string, string, error) {
|
|
|
|
return CreateTokenPairWithDuration(p, time.Minute*15, time.Hour*24*7, sub, id, rId, aud, rAud, perms)
|
2023-06-18 13:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTokenPairWithDuration creates an access and refresh token pair using
|
|
|
|
// custom durations for the access and refresh tokens
|
2023-06-20 00:32:16 +01:00
|
|
|
func CreateTokenPairWithDuration(p mjwt.Signer, accessDur, refreshDur time.Duration, sub, id, rId string, aud, rAud jwt.ClaimStrings, perms *claims.PermStorage) (string, string, error) {
|
|
|
|
accessToken, err := CreateAccessTokenWithDuration(p, accessDur, sub, id, aud, perms)
|
2023-06-18 13:03:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2023-06-20 00:32:16 +01:00
|
|
|
refreshToken, err := CreateRefreshTokenWithDuration(p, refreshDur, sub, rId, id, rAud)
|
2023-06-18 13:03:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return accessToken, refreshToken, nil
|
|
|
|
}
|
2024-06-09 20:31:53 +01:00
|
|
|
|
|
|
|
// CreateTokenPairWithKID creates an access and refresh token pair using the default
|
|
|
|
// 15 minute and 7 day durations respectively using the specified kID
|
|
|
|
func CreateTokenPairWithKID(p mjwt.Signer, sub, id, rId string, aud, rAud jwt.ClaimStrings, perms *claims.PermStorage, kID string) (string, string, error) {
|
|
|
|
return CreateTokenPairWithDurationAndKID(p, time.Minute*15, time.Hour*24*7, sub, id, rId, aud, rAud, perms, kID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTokenPairWithDurationAndKID creates an access and refresh token pair using
|
|
|
|
// custom durations for the access and refresh tokens
|
|
|
|
func CreateTokenPairWithDurationAndKID(p mjwt.Signer, accessDur, refreshDur time.Duration, sub, id, rId string, aud, rAud jwt.ClaimStrings, perms *claims.PermStorage, kID string) (string, string, error) {
|
|
|
|
accessToken, err := CreateAccessTokenWithDurationAndKID(p, accessDur, sub, id, aud, perms, kID)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
refreshToken, err := CreateRefreshTokenWithDurationAndKID(p, refreshDur, sub, rId, id, rAud, kID)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return accessToken, refreshToken, nil
|
|
|
|
}
|