2022-03-30 10:42:47 +02:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 10:42:47 +02:00
|
|
|
|
|
|
|
|
package container
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-08 07:31:33 +02:00
|
|
|
"fmt"
|
2022-03-30 10:42:47 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
2026-05-08 04:07:32 +02:00
|
|
|
auth_model "forgejo.org/models/auth"
|
2025-03-27 19:40:14 +00:00
|
|
|
user_model "forgejo.org/models/user"
|
|
|
|
|
"forgejo.org/modules/log"
|
2026-05-08 04:07:32 +02:00
|
|
|
"forgejo.org/modules/optional"
|
2025-03-27 19:40:14 +00:00
|
|
|
"forgejo.org/services/auth"
|
|
|
|
|
"forgejo.org/services/packages"
|
2022-03-30 10:42:47 +02:00
|
|
|
)
|
|
|
|
|
|
2026-05-08 04:07:32 +02:00
|
|
|
var (
|
|
|
|
|
_ auth.Method = &Auth{}
|
|
|
|
|
_ auth.AuthenticationResult = &containerAuthenticationResult{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type containerAuthenticationResult struct {
|
|
|
|
|
*auth.BaseAuthenticationResult
|
|
|
|
|
user *user_model.User
|
|
|
|
|
scope optional.Option[auth_model.AccessTokenScope]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *containerAuthenticationResult) Scope() optional.Option[auth_model.AccessTokenScope] {
|
|
|
|
|
return r.scope
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *containerAuthenticationResult) User() *user_model.User {
|
|
|
|
|
return r.user
|
|
|
|
|
}
|
2023-09-05 17:58:30 +02:00
|
|
|
|
2022-03-30 10:42:47 +02:00
|
|
|
type Auth struct{}
|
|
|
|
|
|
|
|
|
|
func (a *Auth) Name() string {
|
|
|
|
|
return "container"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify extracts the user from the Bearer token
|
|
|
|
|
// If it's an anonymous session a ghost user is returned
|
2026-05-08 07:31:33 +02:00
|
|
|
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, sess auth.SessionStore) auth.MethodOutput {
|
2024-08-28 08:23:48 +02:00
|
|
|
uid, scope, err := packages.ParseAuthorizationToken(req)
|
2022-03-30 10:42:47 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Trace("ParseAuthorizationToken: %v", err)
|
2026-05-08 07:31:33 +02:00
|
|
|
// Errors from ParseAuthorizationToken are almost all from malformed incoming input, which we'll consider an
|
|
|
|
|
// auth failure:
|
|
|
|
|
// - `Authorization` header was present for all cases, so it's not `AuthenticationNotAttempted`
|
|
|
|
|
// - it's not `AuthenticationError` because malformed headers would cause errors, and this is intended for
|
|
|
|
|
// server errors which should cause 500s
|
|
|
|
|
return &auth.AuthenticationAttemptedIncorrectCredential{Error: err}
|
|
|
|
|
} else if uid == 0 {
|
|
|
|
|
return &auth.AuthenticationNotAttempted{}
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 08:23:48 +02:00
|
|
|
// Propagate scope of the authorization token.
|
2026-05-08 04:07:32 +02:00
|
|
|
authScope := optional.None[auth_model.AccessTokenScope]()
|
2024-08-28 08:23:48 +02:00
|
|
|
if scope != "" {
|
2026-05-08 04:07:32 +02:00
|
|
|
authScope = optional.Some(scope)
|
2024-08-28 08:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-10 16:21:03 +09:00
|
|
|
u, err := user_model.GetPossibleUserByID(req.Context(), uid)
|
2022-03-30 10:42:47 +02:00
|
|
|
if err != nil {
|
2026-05-08 07:31:33 +02:00
|
|
|
return &auth.AuthenticationError{Error: fmt.Errorf("container auth GetPossibleUserByID: %w", err)}
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 07:31:33 +02:00
|
|
|
return &auth.AuthenticationSuccess{Result: &containerAuthenticationResult{user: u, scope: authScope}}
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|