Skip to content

Commit 07e9acc

Browse files
committed
gitsources: implement gitea oauth2 auth
As from go-gitea/gitea#5378 gitea is an oauth2 provider.
1 parent 41618ba commit 07e9acc

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

internal/gitsources/gitea/gitea.go

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package gitea
1616

1717
import (
18+
"context"
1819
"crypto/tls"
1920
"fmt"
2021
"net"
@@ -25,6 +26,7 @@ import (
2526
"time"
2627

2728
gitsource "github.com/sorintlab/agola/internal/gitsources"
29+
"golang.org/x/oauth2"
2830

2931
"code.gitea.io/sdk/gitea"
3032
"github.com/pkg/errors"
@@ -37,14 +39,24 @@ const (
3739
ClientNotFound = "404 Not Found"
3840
)
3941

42+
var (
43+
// gitea corrently doesn't have any auth scope
44+
GiteaOauth2Scopes = []string{""}
45+
)
46+
4047
type Opts struct {
41-
URL string
42-
Token string
43-
SkipVerify bool
48+
URL string
49+
Token string
50+
SkipVerify bool
51+
Oauth2ClientID string
52+
Oauth2Secret string
4453
}
4554

4655
type Client struct {
47-
client *gitea.Client
56+
client *gitea.Client
57+
URL string
58+
oauth2ClientID string
59+
oauth2Secret string
4860
}
4961

5062
// fromCommitStatus converts a gitsource commit status to a gitea commit status
@@ -90,10 +102,47 @@ func New(opts Opts) (*Client, error) {
90102
client.SetHTTPClient(httpClient)
91103

92104
return &Client{
93-
client: client,
105+
client: client,
106+
URL: opts.URL,
107+
oauth2ClientID: opts.Oauth2ClientID,
108+
oauth2Secret: opts.Oauth2Secret,
94109
}, nil
95110
}
96111

112+
func (c *Client) oauth2Config(callbackURL string) *oauth2.Config {
113+
return &oauth2.Config{
114+
ClientID: c.oauth2ClientID,
115+
ClientSecret: c.oauth2Secret,
116+
Scopes: GiteaOauth2Scopes,
117+
Endpoint: oauth2.Endpoint{
118+
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", c.URL),
119+
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", c.URL),
120+
},
121+
RedirectURL: callbackURL,
122+
}
123+
}
124+
125+
func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) {
126+
var config = c.oauth2Config(callbackURL)
127+
return config.AuthCodeURL(state), nil
128+
}
129+
130+
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) {
131+
var config = c.oauth2Config(callbackURL)
132+
token, err := config.Exchange(context.TODO(), code)
133+
if err != nil {
134+
return nil, errors.Wrapf(err, "cannot get oauth2 token")
135+
}
136+
return token, nil
137+
}
138+
139+
func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
140+
var config = c.oauth2Config("")
141+
token := &oauth2.Token{RefreshToken: refreshToken}
142+
ts := config.TokenSource(context.TODO(), token)
143+
return ts.Token()
144+
}
145+
97146
func (c *Client) LoginPassword(username, password, tokenName string) (string, error) {
98147
// try to get agola access token if it already exists
99148
var accessToken string

internal/services/gateway/common/gitsource.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import (
2525

2626
func newGitea(rs *types.RemoteSource, accessToken string) (*gitea.Client, error) {
2727
return gitea.New(gitea.Opts{
28-
URL: rs.APIURL,
29-
SkipVerify: rs.SkipVerify,
30-
Token: accessToken,
28+
URL: rs.APIURL,
29+
SkipVerify: rs.SkipVerify,
30+
Token: accessToken,
31+
Oauth2ClientID: rs.Oauth2ClientID,
32+
Oauth2Secret: rs.Oauth2ClientSecret,
3133
})
3234
}
3335

@@ -95,6 +97,8 @@ func GetOauth2Source(rs *types.RemoteSource, accessToken string) (gitsource.Oaut
9597
var oauth2Source gitsource.Oauth2Source
9698
var err error
9799
switch rs.Type {
100+
case types.RemoteSourceTypeGitea:
101+
oauth2Source, err = newGitea(rs, accessToken)
98102
case types.RemoteSourceTypeGitlab:
99103
oauth2Source, err = newGitlab(rs, accessToken)
100104
default:

internal/services/types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ type RemoteSource struct {
168168
func SourceSupportedAuthTypes(rsType RemoteSourceType) []RemoteSourceAuthType {
169169
switch rsType {
170170
case RemoteSourceTypeGitea:
171-
return []RemoteSourceAuthType{RemoteSourceAuthTypePassword}
171+
return []RemoteSourceAuthType{RemoteSourceAuthTypeOauth2, RemoteSourceAuthTypePassword}
172172
case RemoteSourceTypeGithub:
173173
fallthrough
174174
case RemoteSourceTypeGitlab:

0 commit comments

Comments
 (0)