Skip to content

Commit

Permalink
seperate userdetails to exchange and userinfo
Browse files Browse the repository at this point in the history
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
  • Loading branch information
rodneyosodo committed Mar 14, 2024
1 parent d127701 commit 0e8a8dd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
13 changes: 7 additions & 6 deletions pkg/oauth2/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ func (cfg *config) IsEnabled() bool {
return cfg.config.ClientID != "" && cfg.config.ClientSecret != ""
}

func (cfg *config) UserDetails(ctx context.Context, code string) (mfclients.Client, error) {
func (cfg *config) Exchange(ctx context.Context, code string) (oauth2.Token, error) {
token, err := cfg.config.Exchange(ctx, code)
if err != nil {
return mfclients.Client{}, err
}
if token.RefreshToken == "" {
return mfclients.Client{}, svcerr.ErrAuthentication
return oauth2.Token{}, err
}

resp, err := http.Get(userInfoURL + url.QueryEscape(token.AccessToken))
return *token, nil
}

func (cfg *config) UserInfo(accessToken string) (mfclients.Client, error) {
resp, err := http.Get(userInfoURL + url.QueryEscape(accessToken))
if err != nil {
return mfclients.Client{}, err
}
Expand Down
50 changes: 40 additions & 10 deletions pkg/oauth2/mocks/provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions pkg/oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"

mfclients "github.com/absmach/magistrala/pkg/clients"
"golang.org/x/oauth2"
)

// State is the state of the OAuth2 flow.
Expand Down Expand Up @@ -71,6 +72,9 @@ type Provider interface {
// IsEnabled checks if the OAuth2 provider is enabled.
IsEnabled() bool

// UserDetails retrieves the user's details and OAuth tokens from the OAuth2 provider.
UserDetails(ctx context.Context, code string) (mfclients.Client, error)
// Exchange converts an authorization code into a token.
Exchange(ctx context.Context, code string) (oauth2.Token, error)

// UserInfo retrieves the user's information using the access token.
UserInfo(accessToken string) (mfclients.Client, error)
}
8 changes: 7 additions & 1 deletion users/api/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,13 @@ func oauth2CallbackHandler(oauth oauth2.Provider, svc users.Service) http.Handle
}

if code := r.FormValue("code"); code != "" {
client, err := oauth.UserDetails(r.Context(), code)
token, err := oauth.Exchange(r.Context(), code)
if err != nil {
http.Redirect(w, r, oauth.ErrorURL()+"?error="+err.Error(), http.StatusSeeOther)
return
}

client, err := oauth.UserInfo(token.AccessToken)
if err != nil {
http.Redirect(w, r, oauth.ErrorURL()+"?error="+err.Error(), http.StatusSeeOther)
return
Expand Down

0 comments on commit 0e8a8dd

Please sign in to comment.