An atproto PDS written in Go
1package oauth 2 3import ( 4 "errors" 5 "fmt" 6 "net/url" 7 8 "github.com/haileyok/cocoon/internal/helpers" 9 "github.com/haileyok/cocoon/oauth/constants" 10) 11 12func GenerateCode() string { 13 h, _ := helpers.RandomHex(constants.CodeBytesLength) 14 return constants.CodePrefix + h 15} 16 17func GenerateTokenId() string { 18 h, _ := helpers.RandomHex(constants.TokenIdBytesLength) 19 return constants.TokenIdPrefix + h 20} 21 22func GenerateRefreshToken() string { 23 h, _ := helpers.RandomHex(constants.RefreshTokenBytesLength) 24 return constants.RefreshTokenPrefix + h 25} 26 27func GenerateRequestId() string { 28 h, _ := helpers.RandomHex(constants.RequestIdBytesLength) 29 return constants.RequestIdPrefix + h 30} 31 32func EncodeRequestUri(reqId string) string { 33 return constants.RequestUriPrefix + url.QueryEscape(reqId) 34} 35 36func DecodeRequestUri(reqUri string) (string, error) { 37 if len(reqUri) < len(constants.RequestUriPrefix) { 38 return "", errors.New("invalid request uri") 39 } 40 41 reqIdEnc := reqUri[len(constants.RequestUriPrefix):] 42 reqId, err := url.QueryUnescape(reqIdEnc) 43 if err != nil { 44 return "", fmt.Errorf("could not unescape request id: %w", err) 45 } 46 47 return reqId, nil 48}