|
| 1 | +package authservice |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/database-playground/backend-v2/internal/auth" |
| 11 | + "github.com/database-playground/backend-v2/internal/useraccount" |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | +) |
| 14 | + |
| 15 | +// IntrospectionResponse represents the OAuth 2.0 token introspection response (RFC 7662) |
| 16 | +type IntrospectionResponse struct { |
| 17 | + Active bool `json:"active"` |
| 18 | + Username string `json:"username,omitempty"` // user email |
| 19 | + Scope string `json:"scope,omitempty"` // space-separated scopes |
| 20 | + Sub string `json:"sub,omitempty"` // subject (user ID) |
| 21 | + Exp int64 `json:"exp,omitempty"` // expiration time (Unix timestamp) |
| 22 | + Iat int64 `json:"iat,omitempty"` // issued at (Unix timestamp) |
| 23 | + Azp string `json:"azp,omitempty"` // authorized party (machine name) |
| 24 | +} |
| 25 | + |
| 26 | +// IntrospectToken implements OAuth 2.0 Token Introspection (RFC 7662) |
| 27 | +// POST /api/auth/v2/introspect |
| 28 | +func (s *AuthService) IntrospectToken(c *gin.Context) { |
| 29 | + // Parse form data |
| 30 | + token := c.PostForm("token") |
| 31 | + tokenTypeHint := c.PostForm("token_type_hint") |
| 32 | + |
| 33 | + // Validate required parameters |
| 34 | + if token == "" { |
| 35 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 36 | + "error": "invalid_request", |
| 37 | + "error_description": "Missing required parameter: token", |
| 38 | + }) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + // Validate token_type_hint if provided |
| 43 | + if tokenTypeHint != "" && tokenTypeHint != "access_token" { |
| 44 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 45 | + "error": "unsupported_token_type", |
| 46 | + "error_description": "Only access_token is supported for token_type_hint", |
| 47 | + }) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Try to peek the token (doesn't extend expiration) |
| 52 | + tokenInfo, err := s.storage.Peek(c.Request.Context(), token) |
| 53 | + if err != nil { |
| 54 | + if errors.Is(err, auth.ErrNotFound) { |
| 55 | + // Token not found or expired - return inactive token response |
| 56 | + c.JSON(http.StatusOK, IntrospectionResponse{ |
| 57 | + Active: false, |
| 58 | + }) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // Internal server error |
| 63 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 64 | + "error": "server_error", |
| 65 | + "error_description": "Failed to introspect the token. Please try again later.", |
| 66 | + }) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Get user information |
| 71 | + useraccountCtx := useraccount.NewContext(s.entClient, s.storage) |
| 72 | + entUser, err := useraccountCtx.GetUser(c.Request.Context(), tokenInfo.UserID) |
| 73 | + if err != nil { |
| 74 | + if errors.Is(err, useraccount.ErrUserNotFound) { |
| 75 | + // User not found - token is technically invalid |
| 76 | + c.JSON(http.StatusOK, IntrospectionResponse{ |
| 77 | + Active: false, |
| 78 | + }) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // Internal server error |
| 83 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 84 | + "error": "server_error", |
| 85 | + "error_description": err.Error(), |
| 86 | + }) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + // Calculate token expiration and issue time |
| 91 | + // Note: This is an approximation since we don't store these explicitly |
| 92 | + // We assume token is valid for DefaultTokenExpire seconds from now |
| 93 | + now := time.Now() |
| 94 | + exp := now.Add(time.Duration(auth.DefaultTokenExpire) * time.Second).Unix() |
| 95 | + iat := now.Unix() // Approximation - we don't have the actual issue time |
| 96 | + |
| 97 | + // Build successful introspection response |
| 98 | + response := IntrospectionResponse{ |
| 99 | + Active: true, |
| 100 | + Username: entUser.Email, |
| 101 | + Scope: strings.Join(tokenInfo.Scopes, " "), |
| 102 | + Sub: strconv.Itoa(tokenInfo.UserID), |
| 103 | + Exp: exp, |
| 104 | + Iat: iat, |
| 105 | + Azp: tokenInfo.Machine, |
| 106 | + } |
| 107 | + |
| 108 | + c.JSON(http.StatusOK, response) |
| 109 | +} |
0 commit comments