Files
probo/pkg/server/api/connect/v1/oauth2_error.go
Sacha Al Himdani 4c57d201a4 Make license declarations consistently MIT
The source headers, LICENSE files, and license metadata had drifted
apart. Align the entire project to MIT:

- Convert every source-file header to the MIT text across all comment
  styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including
  SPDX-License-Identifier tags
- Set the root and cookie-banner LICENSE files to the MIT text with a
  "MIT License" title line
- Switch the package.json license fields, Docker image label, and
  cookie-banner README to MIT
- Update docs and the genmodels header generator accordingly
- Normalize copyright lines to a single format
  (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the
  hello@getprobo.com and hello@probo.inc emails to hello@probo.com and
  the comma-separated years to a hyphenated range

Genuine third-party references are intentionally left untouched: the
Lucide icon attributions (Lucide is ISC) and the trivy dependency
license allowlist.

Signed-off-by: Sacha Al Himdani <sacha@probo.com>
2026-07-13 16:21:14 +02:00

131 lines
4.3 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package connect_v1
import (
"errors"
"net/http"
"net/url"
"go.gearno.de/kit/httpserver"
"go.gearno.de/kit/log"
"go.probo.inc/probo/pkg/iam/oauth2"
"go.probo.inc/probo/pkg/server/api/connect/v1/types"
)
func (h *OAuth2Handler) handleAuthorizeError(w http.ResponseWriter, r *http.Request, err error, redirectURI, state string) {
if isRedirectableError(err) && redirectURI != "" {
redirectWithError(w, r, redirectURI, state, err)
return
}
h.renderOAuth2ErrorResponse(w, r, err)
}
func (h *OAuth2Handler) renderOAuth2ErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
oauthErr, ok := errors.AsType[*oauth2.OAuth2Error](err)
if !ok {
httpserver.RenderError(w, http.StatusInternalServerError, err)
return
}
if errors.Is(err, oauth2.ErrServerError) {
h.logger.ErrorCtx(r.Context(), "oauth2 server error", log.Error(err))
}
NoCache(w)
httpserver.RenderJSON(w, oauth2ErrorStatusCode(oauthErr), &types.OAuth2ErrorResponse{
Code: oauthErr.ErrorCode(),
Description: oauthErr.Description(),
})
}
func isRedirectableError(err error) bool {
return errors.Is(err, oauth2.ErrAccessDenied) ||
errors.Is(err, oauth2.ErrInvalidRequest) ||
errors.Is(err, oauth2.ErrInvalidScope) ||
errors.Is(err, oauth2.ErrUnauthorizedClient) ||
errors.Is(err, oauth2.ErrInvalidGrant) ||
errors.Is(err, oauth2.ErrUnsupportedGrantType)
}
func oauth2ErrorStatusCode(err *oauth2.OAuth2Error) int {
switch err.ErrorCode() {
case "access_denied":
return http.StatusForbidden
case "invalid_client":
return http.StatusUnauthorized
case "server_error":
return http.StatusInternalServerError
default:
return http.StatusBadRequest
}
}
func toOAuth2Error(err error) *oauth2.OAuth2Error {
switch {
case errors.Is(err, oauth2.ErrClientNotFound):
return oauth2.NewError(oauth2.ErrInvalidClient, oauth2.WithDescription("client not found"))
case errors.Is(err, oauth2.ErrInvalidRedirectURI):
return oauth2.ErrInvalidRedirectURI
case errors.Is(err, oauth2.ErrUnauthorizedMember):
return oauth2.NewError(oauth2.ErrUnauthorizedClient, oauth2.WithDescription("client is private and user is not a member of the organization"))
case errors.Is(err, oauth2.ErrDeviceCodeNotPending):
return oauth2.NewError(oauth2.ErrInvalidGrant, oauth2.WithDescription("device code is not pending"))
default:
if oauthErr, ok := errors.AsType[*oauth2.OAuth2Error](err); ok {
return oauthErr
}
return oauth2.NewError(oauth2.ErrServerError, oauth2.WithDescription("internal error"))
}
}
func redirectWithError(w http.ResponseWriter, r *http.Request, redirectURI, state string, err error) {
u, parseErr := url.Parse(redirectURI)
if parseErr != nil {
httpserver.RenderError(w, http.StatusInternalServerError, errors.New("internal server error"))
return
}
oauthErr, ok := errors.AsType[*oauth2.OAuth2Error](err)
if !ok {
httpserver.RenderError(w, http.StatusInternalServerError, errors.New("internal server error"))
return
}
q := u.Query()
q.Set("error", oauthErr.ErrorCode())
if desc := oauthErr.Description(); desc != "" {
q.Set("error_description", desc)
}
if state != "" {
q.Set("state", state)
}
u.RawQuery = q.Encode()
http.Redirect(w, r, u.String(), http.StatusFound)
}