Something went wrong. Try again.
An atproto PDS written in Go
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081package oauth
import ( "errors" "fmt" "net/url" "time"
"github.com/haileyok/cocoon/internal/helpers" "github.com/haileyok/cocoon/oauth/constants" "github.com/haileyok/cocoon/oauth/provider")
func GenerateCode() string { h, _ := helpers.RandomHex(constants.CodeBytesLength) return constants.CodePrefix + h}
func GenerateTokenId() string { h, _ := helpers.RandomHex(constants.TokenIdBytesLength) return constants.TokenIdPrefix + h}
func GenerateRefreshToken() string { h, _ := helpers.RandomHex(constants.RefreshTokenBytesLength) return constants.RefreshTokenPrefix + h}
func GenerateRequestId() string { h, _ := helpers.RandomHex(constants.RequestIdBytesLength) return constants.RequestIdPrefix + h}
func EncodeRequestUri(reqId string) string { return constants.RequestUriPrefix + url.QueryEscape(reqId)}
func DecodeRequestUri(reqUri string) (string, error) { if len(reqUri) < len(constants.RequestUriPrefix) { return "", errors.New("invalid request uri") }
reqIdEnc := reqUri[len(constants.RequestUriPrefix):] reqId, err := url.QueryUnescape(reqIdEnc) if err != nil { return "", fmt.Errorf("could not unescape request id: %w", err) }
return reqId, nil}
type SessionAgeResult struct { SessionAge time.Duration RefreshAge time.Duration SessionExpired bool RefreshExpired bool}
func GetSessionAgeFromToken(t provider.OauthToken) SessionAgeResult { sessionLifetime := constants.PublicClientSessionLifetime refreshLifetime := constants.PublicClientRefreshLifetime if t.ClientAuth.Method != "none" { sessionLifetime = constants.ConfidentialClientSessionLifetime refreshLifetime = constants.ConfidentialClientRefreshLifetime }
res := SessionAgeResult{}
res.SessionAge = time.Since(t.CreatedAt) if res.SessionAge > sessionLifetime { res.SessionExpired = true }
refreshAge := time.Since(t.UpdatedAt) if refreshAge > refreshLifetime { res.RefreshExpired = true }
return res}