diff --git a/api/oauth.go b/api/oauth.go index bdccd6a..843f1c0 100644 --- a/api/oauth.go +++ b/api/oauth.go @@ -11,7 +11,7 @@ import ( "time" ) -type oauth struct { +type Oauth struct { cfg *config.Config logger *zap.Logger token *oauth2.Token @@ -19,19 +19,19 @@ type oauth struct { keepAliveRunning bool } -func newOauth(cfg *config.Config, logger *zap.Logger) *oauth { - return &oauth{cfg: cfg, logger: logger} +func NewOauth(cfg *config.Config, logger *zap.Logger) *Oauth { + return &Oauth{cfg: cfg, logger: logger} } -func (o oauth) config() *oauth2.Config { +func (o Oauth) config() *oauth2.Config { cfg := &oauth2.Config{ ClientID: o.cfg.Oauth.ClientId, ClientSecret: o.cfg.Oauth.ClientSecret, Scopes: []string{"admin"}, RedirectURL: fmt.Sprintf("https://%s/setup/callback", o.cfg.Domain), Endpoint: oauth2.Endpoint{ - TokenURL: fmt.Sprintf("%s/login/oauth/access_token", o.cfg.GiteaUrl), - AuthURL: fmt.Sprintf("%s/login/oauth/authorize", o.cfg.GiteaUrl), + TokenURL: fmt.Sprintf("%s/login/Oauth/access_token", o.cfg.GiteaUrl), + AuthURL: fmt.Sprintf("%s/login/Oauth/authorize", o.cfg.GiteaUrl), }, } @@ -41,11 +41,11 @@ func (o oauth) config() *oauth2.Config { return cfg } -func (o oauth) authUrl() string { +func (o Oauth) authUrl() string { return o.config().AuthCodeURL("state") } -func (o oauth) loadToken(config *oauth2.Config) { +func (o Oauth) loadToken(config *oauth2.Config) { token := &oauth2.Token{} if o.cfg.Oauth.Token != "" { @@ -85,7 +85,7 @@ func (o oauth) loadToken(config *oauth2.Config) { o.refresher = config.TokenSource(context.Background(), token) } -func (o oauth) keepAlive() { +func (o Oauth) keepAlive() { if o.cfg.Oauth.Token == "" || o.cfg.Oauth.RefreshToken == "" { o.logger.Error("No token or refresh token provided.") return @@ -112,7 +112,7 @@ func (o oauth) keepAlive() { } }() } -func (o *oauth) isTokenValid() bool { +func (o *Oauth) isTokenValid() bool { if o.token == nil { return false } @@ -120,7 +120,7 @@ func (o *oauth) isTokenValid() bool { return o.token.Valid() } -func (o *oauth) refreshToken() error { +func (o *Oauth) refreshToken() error { o.logger.Info("Refreshing token...") token, err := o.refresher.Token() @@ -133,7 +133,7 @@ func (o *oauth) refreshToken() error { return nil } -func (o *oauth) exchange(code string) (*oauth2.Token, error) { +func (o *Oauth) exchange(code string) (*oauth2.Token, error) { cfg := o.config() token, err := o.config().Exchange(context.Background(), code) if err != nil { @@ -153,7 +153,7 @@ func (o *oauth) exchange(code string) (*oauth2.Token, error) { return token, nil } -func (o *oauth) client() *gitea.Client { +func (o *Oauth) client() *gitea.Client { client, err := getClient(ClientParams{ Config: o.cfg, }) diff --git a/api/routes.go b/api/routes.go index 67d78d9..3b04ed5 100644 --- a/api/routes.go +++ b/api/routes.go @@ -16,6 +16,7 @@ type RouteParams struct { Logger *zap.Logger R *mux.Router WebhookManager *WebhookManager + Oauth *Oauth } func SetupRoutes(params RouteParams) { diff --git a/api/routes_setup.go b/api/routes_setup.go index fea718b..3b287fa 100644 --- a/api/routes_setup.go +++ b/api/routes_setup.go @@ -10,10 +10,10 @@ import ( type setupApi struct { config *config.Config logger *zap.Logger - oauth *oauth + oauth *Oauth } -func newSetupApi(config *config.Config, logger *zap.Logger, oauth *oauth) *setupApi { +func newSetupApi(config *config.Config, logger *zap.Logger, oauth *Oauth) *setupApi { return &setupApi{config: config, logger: logger, oauth: oauth} } @@ -64,7 +64,7 @@ func setupApiRoutes(params RouteParams) { setupRouter := r.PathPrefix("/setup").Subrouter() setupRouter.Use(giteaOauthVerifyMiddleware(params.Config)) - setupApi := newSetupApi(params.Config, params.Logger, newOauth(params.Config, params.Logger)) + setupApi := newSetupApi(params.Config, params.Logger, params.Oauth) setupRouter.HandleFunc("", setupApi.setupHandler).Methods("GET") setupRouter.HandleFunc("/callback", setupApi.callbackHandler).Methods("GET") } diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index d62fb5b..7ba3af2 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -24,6 +24,7 @@ func main() { }), config.Module, db.Module, + fx.Provide(api.NewOauth), fx.Provide(api.NewRouter), fx.Provide(NewServer), fx.Provide(api.NewWebhookManager),