From 60040e90d01a61a73ecc20b2c53c9e1a980466a9 Mon Sep 17 00:00:00 2001 From: Chaim Lev Ari Date: Fri, 18 Jan 2019 10:24:42 +0200 Subject: [PATCH] refactor(oauth): move build url logic to service --- api/http/handler/auth/authenticate_oauth.go | 18 +----------------- api/oauth/oauth.go | 18 ++++++++++++++++++ api/portainer.go | 1 + 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/api/http/handler/auth/authenticate_oauth.go b/api/http/handler/auth/authenticate_oauth.go index 8e7542cb2..43dcdc33c 100644 --- a/api/http/handler/auth/authenticate_oauth.go +++ b/api/http/handler/auth/authenticate_oauth.go @@ -3,9 +3,6 @@ package auth import ( "log" "net/http" - "strings" - - "golang.org/x/oauth2" "github.com/asaskevich/govalidator" httperror "github.com/portainer/libhttp/error" @@ -88,20 +85,7 @@ func (handler *Handler) loginOAuth(w http.ResponseWriter, r *http.Request) *http return &httperror.HandlerError{http.StatusForbidden, "OAuth authentication is disabled", err} } - endpoint := oauth2.Endpoint{ - AuthURL: settings.OAuthSettings.AuthorizationURI, - TokenURL: settings.OAuthSettings.AccessTokenURI, - } - - oauthConfig := &oauth2.Config{ - ClientID: settings.OAuthSettings.ClientID, - ClientSecret: settings.OAuthSettings.ClientSecret, - Endpoint: endpoint, - RedirectURL: settings.OAuthSettings.RedirectURI, - Scopes: strings.Split(settings.OAuthSettings.Scopes, ","), - } - - url := oauthConfig.AuthCodeURL("portainer") + url := handler.OAuthService.BuildLoginURL(settings.OAuthSettings) http.Redirect(w, r, url, http.StatusTemporaryRedirect) return nil } diff --git a/api/oauth/oauth.go b/api/oauth/oauth.go index bafd99443..87439a636 100644 --- a/api/oauth/oauth.go +++ b/api/oauth/oauth.go @@ -165,3 +165,21 @@ func (*Service) GetUsername(token string, settings *portainer.OAuthSettings) (st Body: body, } } + +// BuildLoginURL creates a login url for the oauth provider +func (*Service) BuildLoginURL(oauthSettings portainer.OAuthSettings) string { + endpoint := oauth2.Endpoint{ + AuthURL: oauthSettings.AuthorizationURI, + TokenURL: oauthSettings.AccessTokenURI, + } + + oauthConfig := &oauth2.Config{ + ClientID: oauthSettings.ClientID, + ClientSecret: oauthSettings.ClientSecret, + Endpoint: endpoint, + RedirectURL: oauthSettings.RedirectURI, + Scopes: strings.Split(oauthSettings.Scopes, ","), + } + + return oauthConfig.AuthCodeURL("portainer") +} diff --git a/api/portainer.go b/api/portainer.go index fe099769e..b5682c344 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -766,6 +766,7 @@ type ( OAuthService interface { GetAccessToken(code string, settings *OAuthSettings) (string, error) GetUsername(token string, settings *OAuthSettings) (string, error) + BuildLoginURL(oauthSettings OAuthSettings) string } // SwarmStackManager represents a service to manage Swarm stacks