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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AccessTokenClaims contains the JWT claims for an access token
|
|
|
|
type AccessTokenClaims struct {
|
2023-06-20 00:32:16 +01:00
|
|
|
Perms *claims.PermStorage `json:"per"`
|
2023-06-18 13:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a AccessTokenClaims) Valid() error { return nil }
|
|
|
|
|
|
|
|
func (a AccessTokenClaims) Type() string { return "access-token" }
|
|
|
|
|
|
|
|
// CreateAccessToken creates an access token with the default 15 minute duration
|
2023-06-20 00:32:16 +01:00
|
|
|
func CreateAccessToken(p mjwt.Signer, sub, id string, aud jwt.ClaimStrings, perms *claims.PermStorage) (string, error) {
|
|
|
|
return CreateAccessTokenWithDuration(p, time.Minute*15, sub, id, aud, perms)
|
2023-06-18 13:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAccessTokenWithDuration creates an access token with a custom duration
|
2023-06-20 00:32:16 +01:00
|
|
|
func CreateAccessTokenWithDuration(p mjwt.Signer, dur time.Duration, sub, id string, aud jwt.ClaimStrings, perms *claims.PermStorage) (string, error) {
|
|
|
|
return p.GenerateJwt(sub, id, aud, dur, &AccessTokenClaims{Perms: perms})
|
2023-06-18 13:03:41 +01:00
|
|
|
}
|
2024-06-09 20:31:53 +01:00
|
|
|
|
|
|
|
// CreateAccessTokenWithKID creates an access token with the default 15 minute duration and the specified kID
|
|
|
|
func CreateAccessTokenWithKID(p mjwt.Signer, sub, id string, aud jwt.ClaimStrings, perms *claims.PermStorage, kID string) (string, error) {
|
|
|
|
return CreateAccessTokenWithDurationAndKID(p, time.Minute*15, sub, id, aud, perms, kID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAccessTokenWithDurationAndKID creates an access token with a custom duration and the specified kID
|
|
|
|
func CreateAccessTokenWithDurationAndKID(p mjwt.Signer, dur time.Duration, sub, id string, aud jwt.ClaimStrings, perms *claims.PermStorage, kID string) (string, error) {
|
|
|
|
return p.GenerateJwtWithKID(sub, id, aud, dur, &AccessTokenClaims{Perms: perms}, kID)
|
|
|
|
}
|