Vibe-guided bskyoauth and custom repo example code in Golang 馃 probably not safe to use in prod
at main 4.2 kB view raw
1package bskyoauth 2 3import ( 4 "context" 5 "errors" 6 "net" 7 "os" 8 9 "github.com/shindakun/bskyoauth/internal/jwt" 10 "github.com/shindakun/bskyoauth/internal/session" 11 "github.com/shindakun/bskyoauth/internal/validation" 12) 13 14// Client Errors 15var ( 16 // ErrNoSession is returned when no valid session is available 17 ErrNoSession = errors.New("no valid session") 18) 19 20// OAuth Errors 21var ( 22 // ErrInvalidHandle is returned when the handle cannot be parsed or resolved 23 ErrInvalidHandle = errors.New("invalid handle") 24 25 // ErrInvalidState is returned when the OAuth state parameter is invalid 26 ErrInvalidState = errors.New("invalid state parameter") 27 28 // ErrTokenExchange is returned when token exchange fails 29 ErrTokenExchange = errors.New("token exchange failed") 30 31 // ErrNoAccessToken is returned when no access token is received 32 ErrNoAccessToken = errors.New("no access token in response") 33 34 // ErrTokenRefreshFailed is returned when token refresh fails 35 ErrTokenRefreshFailed = errors.New("token refresh failed") 36 37 // ErrIssuerMismatch is returned when the issuer doesn't match expected value 38 ErrIssuerMismatch = errors.New("issuer mismatch") 39 40 // ErrMetadataFetch is returned when OAuth server metadata cannot be fetched 41 ErrMetadataFetch = errors.New("failed to fetch OAuth server metadata") 42) 43 44// JWT Errors 45// Re-exported from internal/jwt for backward compatibility 46var ( 47 // ErrInvalidToken indicates the access token is invalid. 48 ErrInvalidToken = jwt.ErrInvalidToken 49 50 // ErrTokenExpired indicates the access token has expired. 51 ErrTokenExpired = jwt.ErrTokenExpired 52 53 // ErrInvalidSignature indicates the token signature is invalid. 54 ErrInvalidSignature = jwt.ErrInvalidSignature 55 56 // ErrInvalidIssuer indicates the token issuer doesn't match expected value. 57 ErrInvalidIssuer = jwt.ErrInvalidIssuer 58 59 // ErrJWKSFetch indicates failure to fetch JWKS from the authorization server. 60 ErrJWKSFetch = jwt.ErrJWKSFetch 61 62 // ErrInvalidAlgorithm indicates the token uses an unsupported algorithm. 63 ErrInvalidAlgorithm = jwt.ErrInvalidAlgorithm 64 65 // ErrMissingClaims indicates required claims are missing from the token. 66 ErrMissingClaims = jwt.ErrMissingClaims 67) 68 69// Session Errors 70// Re-exported from internal/session for backward compatibility 71var ( 72 // ErrSessionNotFound is returned when a session ID doesn't exist 73 ErrSessionNotFound = session.ErrSessionNotFound 74 75 // ErrInvalidSession is returned when a session is invalid or expired 76 ErrInvalidSession = session.ErrInvalidSession 77) 78 79// Validation Errors 80// Re-exported from internal/validation for backward compatibility 81var ( 82 // ErrHandleInvalid is returned when handle format is invalid 83 ErrHandleInvalid = validation.ErrHandleInvalid 84 85 // ErrHandleTooLong is returned when handle exceeds maximum length 86 ErrHandleTooLong = validation.ErrHandleTooLong 87 88 // ErrTextEmpty is returned when text cannot be empty 89 ErrTextEmpty = validation.ErrTextEmpty 90 91 // ErrTextTooLong is returned when text exceeds maximum length 92 ErrTextTooLong = validation.ErrTextTooLong 93 94 // ErrInvalidUTF8 is returned when text contains invalid UTF-8 95 ErrInvalidUTF8 = validation.ErrInvalidUTF8 96 97 // ErrNullByte is returned when text contains null bytes 98 ErrNullByte = validation.ErrNullByte 99 100 // ErrRecordFieldInvalid is returned when record field is invalid 101 ErrRecordFieldInvalid = validation.ErrRecordFieldInvalid 102 103 // ErrInvalidDatetime is returned when datetime format is invalid 104 ErrInvalidDatetime = validation.ErrInvalidDatetime 105 106 // ErrInvalidCollection is returned when collection NSID is invalid 107 ErrInvalidCollection = validation.ErrInvalidCollection 108) 109 110// IsTimeoutError checks if an error is a timeout error. 111// Returns true for context deadline exceeded, network timeouts, and OS deadline exceeded. 112func IsTimeoutError(err error) bool { 113 if err == nil { 114 return false 115 } 116 117 // Check for context deadline exceeded 118 if errors.Is(err, context.DeadlineExceeded) { 119 return true 120 } 121 122 // Check for net.Error timeout 123 var netErr net.Error 124 if errors.As(err, &netErr) && netErr.Timeout() { 125 return true 126 } 127 128 // Check for os.ErrDeadlineExceeded 129 if errors.Is(err, os.ErrDeadlineExceeded) { 130 return true 131 } 132 133 return false 134}