|
| 1 | +package oauthserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-chi/chi/v5" |
| 10 | + "github.com/supabase/auth/internal/api/apierrors" |
| 11 | + "github.com/supabase/auth/internal/api/shared" |
| 12 | + "github.com/supabase/auth/internal/models" |
| 13 | + "github.com/supabase/auth/internal/observability" |
| 14 | +) |
| 15 | + |
| 16 | +// OAuthServerClientResponse represents the response format for OAuth client operations |
| 17 | +type OAuthServerClientResponse struct { |
| 18 | + ClientID string `json:"client_id"` |
| 19 | + ClientSecret string `json:"client_secret,omitempty"` // only returned on registration |
| 20 | + |
| 21 | + RedirectURIs []string `json:"redirect_uris"` |
| 22 | + TokenEndpointAuthMethod []string `json:"token_endpoint_auth_method"` |
| 23 | + GrantTypes []string `json:"grant_types"` |
| 24 | + ResponseTypes []string `json:"response_types"` |
| 25 | + ClientName string `json:"client_name,omitempty"` |
| 26 | + ClientURI string `json:"client_uri,omitempty"` |
| 27 | + LogoURI string `json:"logo_uri,omitempty"` |
| 28 | + |
| 29 | + // Metadata fields |
| 30 | + RegistrationType string `json:"registration_type"` |
| 31 | + CreatedAt time.Time `json:"created_at"` |
| 32 | + UpdatedAt time.Time `json:"updated_at"` |
| 33 | +} |
| 34 | + |
| 35 | +// OAuthServerClientListResponse represents the response for listing OAuth clients |
| 36 | +type OAuthServerClientListResponse struct { |
| 37 | + Clients []OAuthServerClientResponse `json:"clients"` |
| 38 | +} |
| 39 | + |
| 40 | +// oauthServerClientToResponse converts a model to response format |
| 41 | +func oauthServerClientToResponse(client *models.OAuthServerClient, includeSecret bool) *OAuthServerClientResponse { |
| 42 | + response := &OAuthServerClientResponse{ |
| 43 | + ClientID: client.ClientID, |
| 44 | + |
| 45 | + // OAuth 2.1 DCR fields |
| 46 | + RedirectURIs: client.GetRedirectURIs(), |
| 47 | + TokenEndpointAuthMethod: []string{"client_secret_basic", "client_secret_post"}, // Both methods are supported |
| 48 | + GrantTypes: client.GetGrantTypes(), |
| 49 | + ResponseTypes: []string{"code"}, // Always "code" in OAuth 2.1 |
| 50 | + ClientName: client.ClientName.String(), |
| 51 | + ClientURI: client.ClientURI.String(), |
| 52 | + LogoURI: client.LogoURI.String(), |
| 53 | + |
| 54 | + // Metadata fields |
| 55 | + RegistrationType: client.RegistrationType, |
| 56 | + CreatedAt: client.CreatedAt, |
| 57 | + UpdatedAt: client.UpdatedAt, |
| 58 | + } |
| 59 | + |
| 60 | + // Only include client_secret during registration |
| 61 | + if includeSecret { |
| 62 | + // Note: This will be filled in by the handler with the plaintext secret |
| 63 | + response.ClientSecret = "" |
| 64 | + } |
| 65 | + |
| 66 | + return response |
| 67 | +} |
| 68 | + |
| 69 | +// LoadOAuthServerClient is middleware that loads an OAuth server client from the URL parameter |
| 70 | +func (s *Server) LoadOAuthServerClient(w http.ResponseWriter, r *http.Request) (context.Context, error) { |
| 71 | + ctx := r.Context() |
| 72 | + clientID := chi.URLParam(r, "client_id") |
| 73 | + |
| 74 | + if clientID == "" { |
| 75 | + return nil, apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "client_id is required") |
| 76 | + } |
| 77 | + |
| 78 | + observability.LogEntrySetField(r, "oauth_client_id", clientID) |
| 79 | + |
| 80 | + client, err := s.getOAuthServerClient(ctx, clientID) |
| 81 | + if err != nil { |
| 82 | + if models.IsNotFoundError(err) { |
| 83 | + return nil, apierrors.NewNotFoundError(apierrors.ErrorCodeUserNotFound, "OAuth client not found") |
| 84 | + } |
| 85 | + return nil, apierrors.NewInternalServerError("Error loading OAuth client").WithInternalError(err) |
| 86 | + } |
| 87 | + |
| 88 | + ctx = WithOAuthServerClient(ctx, client) |
| 89 | + return ctx, nil |
| 90 | +} |
| 91 | + |
| 92 | +// AdminOAuthServerClientRegister handles POST /admin/oauth/clients (manual registration by admins) |
| 93 | +func (s *Server) AdminOAuthServerClientRegister(w http.ResponseWriter, r *http.Request) error { |
| 94 | + ctx := r.Context() |
| 95 | + |
| 96 | + var params OAuthServerClientRegisterParams |
| 97 | + if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { |
| 98 | + return apierrors.NewBadRequestError(apierrors.ErrorCodeBadJSON, "Invalid JSON body") |
| 99 | + } |
| 100 | + |
| 101 | + // Force registration type to manual for admin endpoint |
| 102 | + params.RegistrationType = "manual" |
| 103 | + |
| 104 | + client, plaintextSecret, err := s.registerOAuthServerClient(ctx, ¶ms) |
| 105 | + if err != nil { |
| 106 | + return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, err.Error()) |
| 107 | + } |
| 108 | + |
| 109 | + response := oauthServerClientToResponse(client, true) |
| 110 | + response.ClientSecret = plaintextSecret |
| 111 | + |
| 112 | + return shared.SendJSON(w, http.StatusCreated, response) |
| 113 | +} |
| 114 | + |
| 115 | +// OAuthServerClientDynamicRegister handles POST /oauth/register (OAuth 2.1 Dynamic Client Registration) |
| 116 | +func (s *Server) OAuthServerClientDynamicRegister(w http.ResponseWriter, r *http.Request) error { |
| 117 | + ctx := r.Context() |
| 118 | + |
| 119 | + // Check if dynamic registration is enabled |
| 120 | + if !s.config.OAuthServer.AllowDynamicRegistration { |
| 121 | + return apierrors.NewForbiddenError(apierrors.ErrorCodeOAuthDynamicClientRegistrationDisabled, "Dynamic client registration is not enabled") |
| 122 | + } |
| 123 | + |
| 124 | + var params OAuthServerClientRegisterParams |
| 125 | + if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { |
| 126 | + return apierrors.NewBadRequestError(apierrors.ErrorCodeBadJSON, "Invalid JSON body") |
| 127 | + } |
| 128 | + |
| 129 | + params.RegistrationType = "dynamic" |
| 130 | + |
| 131 | + client, plaintextSecret, err := s.registerOAuthServerClient(ctx, ¶ms) |
| 132 | + if err != nil { |
| 133 | + return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, err.Error()) |
| 134 | + } |
| 135 | + |
| 136 | + response := oauthServerClientToResponse(client, true) |
| 137 | + response.ClientSecret = plaintextSecret |
| 138 | + |
| 139 | + return shared.SendJSON(w, http.StatusCreated, response) |
| 140 | +} |
| 141 | + |
| 142 | +// OAuthServerClientGet handles GET /admin/oauth/clients/{client_id} |
| 143 | +func (s *Server) OAuthServerClientGet(w http.ResponseWriter, r *http.Request) error { |
| 144 | + ctx := r.Context() |
| 145 | + client := GetOAuthServerClient(ctx) |
| 146 | + |
| 147 | + response := oauthServerClientToResponse(client, false) |
| 148 | + return shared.SendJSON(w, http.StatusOK, response) |
| 149 | +} |
| 150 | + |
| 151 | +// OAuthServerClientDelete handles DELETE /admin/oauth/clients/{client_id} |
| 152 | +func (s *Server) OAuthServerClientDelete(w http.ResponseWriter, r *http.Request) error { |
| 153 | + ctx := r.Context() |
| 154 | + client := GetOAuthServerClient(ctx) |
| 155 | + |
| 156 | + if err := s.deleteOAuthServerClient(ctx, client.ClientID); err != nil { |
| 157 | + return apierrors.NewInternalServerError("Error deleting OAuth client").WithInternalError(err) |
| 158 | + } |
| 159 | + |
| 160 | + w.WriteHeader(http.StatusNoContent) |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +// OAuthServerClientList handles GET /admin/oauth/clients |
| 165 | +func (s *Server) OAuthServerClientList(w http.ResponseWriter, r *http.Request) error { |
| 166 | + ctx := r.Context() |
| 167 | + db := s.db.WithContext(ctx) |
| 168 | + |
| 169 | + var clients []models.OAuthServerClient |
| 170 | + if err := db.Q().Where("deleted_at is null").Order("created_at desc").All(&clients); err != nil { |
| 171 | + return apierrors.NewInternalServerError("Error listing OAuth clients").WithInternalError(err) |
| 172 | + } |
| 173 | + |
| 174 | + responses := make([]OAuthServerClientResponse, len(clients)) |
| 175 | + for i, client := range clients { |
| 176 | + responses[i] = *oauthServerClientToResponse(&client, false) |
| 177 | + } |
| 178 | + |
| 179 | + response := OAuthServerClientListResponse{ |
| 180 | + Clients: responses, |
| 181 | + } |
| 182 | + |
| 183 | + return shared.SendJSON(w, http.StatusOK, response) |
| 184 | +} |
0 commit comments