2021-06-09 17:53:16 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-09 17:53:16 +00:00
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2022-08-28 09:43:25 +00:00
|
|
|
"context"
|
2021-06-09 17:53:16 +00:00
|
|
|
"net/http"
|
2022-03-28 04:46:28 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-06-09 17:53:16 +00:00
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-09 17:53:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure the struct implements the interface.
|
|
|
|
var (
|
2021-07-24 10:16:34 +00:00
|
|
|
_ Method = &Group{}
|
|
|
|
_ Initializable = &Group{}
|
|
|
|
_ Freeable = &Group{}
|
2021-06-09 17:53:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Group implements the Auth interface with serval Auth.
|
|
|
|
type Group struct {
|
2021-07-24 10:16:34 +00:00
|
|
|
methods []Method
|
2021-06-09 17:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGroup creates a new auth group
|
2021-07-24 10:16:34 +00:00
|
|
|
func NewGroup(methods ...Method) *Group {
|
2021-06-09 17:53:16 +00:00
|
|
|
return &Group{
|
|
|
|
methods: methods,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:46:28 +00:00
|
|
|
// Add adds a new method to group
|
|
|
|
func (b *Group) Add(method Method) {
|
|
|
|
b.methods = append(b.methods, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns group's methods name
|
|
|
|
func (b *Group) Name() string {
|
|
|
|
names := make([]string, 0, len(b.methods))
|
|
|
|
for _, m := range b.methods {
|
|
|
|
if n, ok := m.(Named); ok {
|
|
|
|
names = append(names, n.Name())
|
|
|
|
} else {
|
|
|
|
names = append(names, reflect.TypeOf(m).Elem().Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(names, ",")
|
|
|
|
}
|
|
|
|
|
2021-06-09 17:53:16 +00:00
|
|
|
// Init does nothing as the Basic implementation does not need to allocate any resources
|
2022-08-28 09:43:25 +00:00
|
|
|
func (b *Group) Init(ctx context.Context) error {
|
2021-07-24 10:16:34 +00:00
|
|
|
for _, method := range b.methods {
|
|
|
|
initializable, ok := method.(Initializable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-28 09:43:25 +00:00
|
|
|
if err := initializable.Init(ctx); err != nil {
|
2021-06-09 17:53:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free does nothing as the Basic implementation does not have to release any resources
|
|
|
|
func (b *Group) Free() error {
|
2021-07-24 10:16:34 +00:00
|
|
|
for _, method := range b.methods {
|
|
|
|
freeable, ok := method.(Freeable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := freeable.Free(); err != nil {
|
2021-06-09 17:53:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify extracts and validates
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-28 05:53:28 +00:00
|
|
|
func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
|
2021-06-09 17:53:16 +00:00
|
|
|
// Try to sign in with each of the enabled plugins
|
2023-07-12 10:18:27 +00:00
|
|
|
var retErr error
|
2021-06-09 17:53:16 +00:00
|
|
|
for _, ssoMethod := range b.methods {
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-28 05:53:28 +00:00
|
|
|
user, err := ssoMethod.Verify(req, w, store, sess)
|
|
|
|
if err != nil {
|
2023-07-12 10:18:27 +00:00
|
|
|
if retErr == nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
// Try other methods if this one failed.
|
|
|
|
// Some methods may share the same protocol to detect if they are matched.
|
|
|
|
// For example, OAuth2 and conan.Auth both read token from "Authorization: Bearer <token>" header,
|
|
|
|
// If OAuth2 returns error, we should give conan.Auth a chance to try.
|
|
|
|
continue
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-28 05:53:28 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 10:18:27 +00:00
|
|
|
// If any method returns a user, we can stop trying.
|
|
|
|
// Return the user and ignore any error returned by previous methods.
|
2021-06-09 17:53:16 +00:00
|
|
|
if user != nil {
|
|
|
|
if store.GetData()["AuthedMethod"] == nil {
|
2021-07-24 10:16:34 +00:00
|
|
|
if named, ok := ssoMethod.(Named); ok {
|
|
|
|
store.GetData()["AuthedMethod"] = named.Name()
|
|
|
|
}
|
2021-06-09 17:53:16 +00:00
|
|
|
}
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-28 05:53:28 +00:00
|
|
|
return user, nil
|
2021-06-09 17:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 10:18:27 +00:00
|
|
|
// If no method returns a user, return the error returned by the first method.
|
|
|
|
return nil, retErr
|
2021-06-09 17:53:16 +00:00
|
|
|
}
|