Some checks failed
Build and Release / Create Release (push) Successful in 0s
Trigger Vault Plugin Rebuild / Trigger Vault Rebuild (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 2m48s
Build and Release / Lint (push) Failing after 5m2s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, linux, linux-latest) (push) Has been skipped
Build and Release / Unit Tests (push) Successful in 5m37s
Go's semantic import versioning requires v2+ modules to include the major version in the module path. This enables using proper version tags (v3.x.x) instead of pseudo-versions. Updated module path: code.gitcaddy.com/server/v3
80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package oauth2
|
|
|
|
import (
|
|
"context"
|
|
"encoding/gob"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"code.gitcaddy.com/server/v3/models/auth"
|
|
"code.gitcaddy.com/server/v3/models/db"
|
|
"code.gitcaddy.com/server/v3/modules/log"
|
|
"code.gitcaddy.com/server/v3/modules/optional"
|
|
"code.gitcaddy.com/server/v3/modules/setting"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/sessions"
|
|
"github.com/markbates/goth/gothic"
|
|
)
|
|
|
|
var gothRWMutex = sync.RWMutex{}
|
|
|
|
// ProviderHeaderKey is the HTTP header key
|
|
const ProviderHeaderKey = "gitea-oauth2-provider"
|
|
|
|
// Init initializes the oauth source
|
|
func Init(ctx context.Context) error {
|
|
// Lock our mutex
|
|
gothRWMutex.Lock()
|
|
|
|
gob.Register(&sessions.Session{}) // TODO: CHI-SESSION-GOB-REGISTER. FIXME: it seems to be an abuse, why the Session struct itself is stored in session store again?
|
|
|
|
gothic.Store = &SessionsStore{
|
|
maxLength: int64(setting.OAuth2.MaxTokenLength),
|
|
}
|
|
|
|
gothic.SetState = func(req *http.Request) string {
|
|
return uuid.New().String()
|
|
}
|
|
|
|
gothic.GetProviderName = func(req *http.Request) (string, error) {
|
|
return req.Header.Get(ProviderHeaderKey), nil
|
|
}
|
|
|
|
// Unlock our mutex
|
|
gothRWMutex.Unlock()
|
|
|
|
return initOAuth2Sources(ctx)
|
|
}
|
|
|
|
// ResetOAuth2 clears existing OAuth2 providers and loads them from DB
|
|
func ResetOAuth2(ctx context.Context) error {
|
|
ClearProviders()
|
|
return initOAuth2Sources(ctx)
|
|
}
|
|
|
|
// initOAuth2Sources is used to load and register all active OAuth2 providers
|
|
func initOAuth2Sources(ctx context.Context) error {
|
|
authSources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
|
|
IsActive: optional.Some(true),
|
|
LoginType: auth.OAuth2,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, source := range authSources {
|
|
oauth2Source, ok := source.Cfg.(*Source)
|
|
if !ok {
|
|
continue
|
|
}
|
|
err := oauth2Source.RegisterSource()
|
|
if err != nil {
|
|
log.Critical("Unable to register source: %s due to Error: %v.", source.Name, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|