Files
sigma/pkg/api/oauth2.go
T
Tosone 5ca747ddd7 refactor(oauth2): stop persisting OAuth tokens on login
The callback stored the access and refresh token on user_3rdparty and
re-issued the code repository sync on every login, which only worked
because the login refreshed an expiring token. Credentials now live in the
clone credential table, so the tokens have no reader left.

- drop the token update on repeated login and the token fields from both
  binding-creation paths; Oauth2UserInfo no longer carries them
- stop publishing a sync task when a binding is created: there is no
  credential at that point, so the task could only report a failure
- derive the GitLab and Gitea OAuth endpoints from scm.providers[].baseUrl
  instead of hardcoding gitlab.com, and point Gitea at its own
  /login/oauth endpoints and redirect callback

Verification:
- CI_DATABASE_TYPE=turso go test -tags "netgo,timetzdata,exclude_graphdriver_btrfs,containers_image_openpgp" ./pkg/service/oauth2/...
2026-10-27 21:07:40 +08:00

62 lines
2.5 KiB
Go

// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import "github.com/go-sigma/sigma/pkg/api/enums"
// Oauth2LoginRequest is the request for starting an OAuth2 login flow.
type Oauth2LoginRequest struct {
Provider enums.Provider `json:"provider" param:"provider" validate:"required,is_valid_provider"`
}
// Oauth2CallbackRequest is the request for handling an OAuth2 callback.
type Oauth2CallbackRequest struct {
Provider enums.Provider `json:"provider" param:"provider" validate:"required,is_valid_provider" example:"gitlab"`
Code string `json:"code" query:"code" validate:"required" example:"123456"`
Endpoint string `json:"endpoint" query:"endpoint" example:"http://localhost:5173"`
}
// Oauth2RedirectCallbackRequest is the request for handling an OAuth2 redirect callback.
type Oauth2RedirectCallbackRequest struct {
Code string `json:"code" query:"code"`
}
// Oauth2UserInfo represents user information returned by an OAuth2 provider.
type Oauth2UserInfo struct {
Provider enums.Provider `json:"provider"`
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
}
// Oauth2CallbackResponse is the response returned after a successful OAuth2 callback.
type Oauth2CallbackResponse struct {
RefreshToken string `json:"refresh_token" example:"sample-refresh-token"`
Token string `json:"token" example:"sample-access-token"`
ID string `json:"id" example:"550e8400-e29b-41d4-a716-446655440000"`
Email string `json:"email" example:"test@email.com"`
Username string `json:"username" example:"sigma"`
}
// Oauth2ClientIDRequest is the request for getting an OAuth2 client ID.
type Oauth2ClientIDRequest struct {
Provider enums.Provider `json:"provider" param:"provider" validate:"required,is_valid_provider"`
}
// Oauth2ClientIDResponse is the response containing an OAuth2 client ID.
type Oauth2ClientIDResponse struct {
ClientID string `json:"client_id" example:"1234567890"`
}