Refactor: remove Auth() from Forge interface (#6505)

This commit is contained in:
6543
2026-04-26 15:36:12 +02:00
committed by GitHub
parent 26c793f650
commit d34649de22
11 changed files with 0 additions and 176 deletions
-5
View File
@@ -18,11 +18,6 @@ import (
"go.woodpecker-ci.org/woodpecker/v3/server/model"
)
type argumentsAuth struct {
Token string `json:"token"`
Secret string `json:"secret"`
}
type argumentsRepo struct {
U *modelUser `json:"u"`
RemoteID model.ForgeRemoteID `json:"remote_id"`
-12
View File
@@ -103,18 +103,6 @@ func (g *RPC) Login(_ context.Context, r *types.OAuthRequest) (*model.User, stri
return resp.User.asModel(), resp.RedirectURL, nil
}
func (g *RPC) Auth(_ context.Context, token, secret string) (string, error) {
args, err := json.Marshal(&argumentsAuth{
Token: token,
Secret: secret,
})
if err != nil {
return "", err
}
var resp string
return resp, g.client.Call("Plugin.Auth", args, &resp)
}
func (g *RPC) Teams(_ context.Context, u *model.User, p *model.ListOptions) ([]*model.Team, error) {
args, err := json.Marshal(&argumentsTeams{
U: modelUserFromModel(u),
-11
View File
@@ -119,17 +119,6 @@ func (c *config) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mod
return convertUser(curr, token, primaryEmail), redirectURL, nil
}
// Auth uses the Bitbucket oauth2 access token and refresh token to authenticate
// a session and return the Bitbucket account login.
func (c *config) Auth(ctx context.Context, token, secret string) (string, error) {
client := c.newClientToken(ctx, token, secret)
user, err := client.FindCurrent()
if err != nil {
return "", err
}
return user.Login, nil
}
// Refresh refreshes the Bitbucket oauth2 access token. If the token is
// refreshed the user is updated and a true value is returned.
func (c *config) Refresh(ctx context.Context, user *model.User) (bool, error) {
-12
View File
@@ -81,13 +81,6 @@ func TestBitbucket(t *testing.T) {
})
assert.Error(t, err)
login, err := c.Auth(ctx, fakeUser.AccessToken, fakeUser.RefreshToken)
assert.NoError(t, err)
assert.Equal(t, fakeUser.Login, login)
_, err = c.Auth(ctx, fakeUserNotFound.AccessToken, fakeUserNotFound.RefreshToken)
assert.Error(t, err)
ok, err := c.Refresh(ctx, fakeUserRefresh)
assert.NoError(t, err)
assert.True(t, ok)
@@ -241,11 +234,6 @@ var (
RefreshToken: "refresh_token_is_empty",
}
fakeUserNotFound = &model.User{
Login: "superman",
AccessToken: "user_not_found",
}
fakeUserNoTeams = &model.User{
Login: "superman",
AccessToken: "teams_not_found",
@@ -142,15 +142,6 @@ func (c *client) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mod
return u, "", nil
}
func (c *client) Auth(ctx context.Context, accessToken, _ string) (string, error) {
config := c.newOAuth2Config()
token := &oauth2.Token{
AccessToken: accessToken,
}
client := internal.NewClientWithToken(ctx, config.TokenSource(ctx, token), c.url)
return client.FindCurrentUser(ctx)
}
func (c *client) Refresh(ctx context.Context, u *model.User) (bool, error) {
config := c.newOAuth2Config()
t := &oauth2.Token{
-3
View File
@@ -73,9 +73,6 @@ type Forge interface {
// Returned User must contain: Login, Email, Avatar, AccessToken, RefreshToken, Expiry, ForgeRemoteID
Login(ctx context.Context, r *types.OAuthRequest) (*model.User, string, error)
// Auth validates an access token and returns the associated username.
Auth(ctx context.Context, token, secret string) (string, error)
// Teams fetches all team/organization memberships for a user.
// Used to determine if an user is member of an team/organization.
// Should support pagination via ListOptions.
-14
View File
@@ -144,20 +144,6 @@ func (c *Forgejo) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mo
}, redirectURL, nil
}
// Auth uses the Forgejo oauth2 access token and refresh token to authenticate
// a session and return the Forgejo account login.
func (c *Forgejo) Auth(ctx context.Context, token, _ string) (string, error) {
client, err := c.newClientToken(ctx, token)
if err != nil {
return "", err
}
user, _, err := client.GetMyUserInfo()
if err != nil {
return "", err
}
return user.UserName, nil
}
// Refresh refreshes the Forgejo oauth2 access token. If the token is
// refreshed, the user is updated and a true value is returned.
func (c *Forgejo) Refresh(ctx context.Context, user *model.User) (bool, error) {
-14
View File
@@ -146,20 +146,6 @@ func (c *Gitea) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mode
}, redirectURL, nil
}
// Auth uses the Gitea oauth2 access token and refresh token to authenticate
// a session and return the Gitea account login.
func (c *Gitea) Auth(ctx context.Context, token, _ string) (string, error) {
client, err := c.newClientToken(ctx, token)
if err != nil {
return "", err
}
user, _, err := client.GetMyUserInfo()
if err != nil {
return "", err
}
return user.UserName, nil
}
// Refresh refreshes the Gitea oauth2 access token. If the token is
// refreshed, the user is updated and a true value is returned.
func (c *Gitea) Refresh(ctx context.Context, user *model.User) (bool, error) {
-10
View File
@@ -149,16 +149,6 @@ func (c *client) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mod
}, redirectURL, nil
}
// Auth returns the GitHub user login for the given access token.
func (c *client) Auth(ctx context.Context, token, _ string) (string, error) {
client := c.newClientToken(ctx, token)
user, _, err := client.Users.Get(ctx, "")
if err != nil {
return "", err
}
return *user.Login, nil
}
// Refresh refreshes the Gitlab oauth2 access token. If the token is
// refreshed the user is updated and a true value is returned.
func (c *client) Refresh(ctx context.Context, user *model.User) (bool, error) {
-14
View File
@@ -178,20 +178,6 @@ func (g *GitLab) Refresh(ctx context.Context, user *model.User) (bool, error) {
return true, nil
}
// Auth authenticates the session and returns the forge user login for the given token.
func (g *GitLab) Auth(ctx context.Context, token, _ string) (string, error) {
client, err := newClient(g.url, token, g.skipVerify)
if err != nil {
return "", err
}
login, _, err := client.Users.CurrentUser(gitlab.WithContext(ctx))
if err != nil {
return "", err
}
return login.Username, nil
}
// Teams fetches a list of team memberships from the forge.
func (g *GitLab) Teams(ctx context.Context, user *model.User, p *model.ListOptions) ([]*model.Team, error) {
client, err := newClient(g.url, user.AccessToken, g.skipVerify)
-72
View File
@@ -109,78 +109,6 @@ func (_c *MockForge_Activate_Call) RunAndReturn(run func(ctx context.Context, u
return _c
}
// Auth provides a mock function for the type MockForge
func (_mock *MockForge) Auth(ctx context.Context, token string, secret string) (string, error) {
ret := _mock.Called(ctx, token, secret)
if len(ret) == 0 {
panic("no return value specified for Auth")
}
var r0 string
var r1 error
if returnFunc, ok := ret.Get(0).(func(context.Context, string, string) (string, error)); ok {
return returnFunc(ctx, token, secret)
}
if returnFunc, ok := ret.Get(0).(func(context.Context, string, string) string); ok {
r0 = returnFunc(ctx, token, secret)
} else {
r0 = ret.Get(0).(string)
}
if returnFunc, ok := ret.Get(1).(func(context.Context, string, string) error); ok {
r1 = returnFunc(ctx, token, secret)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockForge_Auth_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Auth'
type MockForge_Auth_Call struct {
*mock.Call
}
// Auth is a helper method to define mock.On call
// - ctx context.Context
// - token string
// - secret string
func (_e *MockForge_Expecter) Auth(ctx interface{}, token interface{}, secret interface{}) *MockForge_Auth_Call {
return &MockForge_Auth_Call{Call: _e.mock.On("Auth", ctx, token, secret)}
}
func (_c *MockForge_Auth_Call) Run(run func(ctx context.Context, token string, secret string)) *MockForge_Auth_Call {
_c.Call.Run(func(args mock.Arguments) {
var arg0 context.Context
if args[0] != nil {
arg0 = args[0].(context.Context)
}
var arg1 string
if args[1] != nil {
arg1 = args[1].(string)
}
var arg2 string
if args[2] != nil {
arg2 = args[2].(string)
}
run(
arg0,
arg1,
arg2,
)
})
return _c
}
func (_c *MockForge_Auth_Call) Return(s string, err error) *MockForge_Auth_Call {
_c.Call.Return(s, err)
return _c
}
func (_c *MockForge_Auth_Call) RunAndReturn(run func(ctx context.Context, token string, secret string) (string, error)) *MockForge_Auth_Call {
_c.Call.Return(run)
return _c
}
// BranchHead provides a mock function for the type MockForge
func (_mock *MockForge) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
ret := _mock.Called(ctx, u, r, branch)