Skip to content

Commit

Permalink
Added state that contains a json base64 encoded with the request stat…
Browse files Browse the repository at this point in the history
…e params
  • Loading branch information
nigeon committed Nov 9, 2023
1 parent 2ec78ed commit 3334e59
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ func (f *faucet) authOAuthUrl(msg *apirest.APIdata, ctx *httprouter.HTTPContext)

type r struct {
RedirectURL string `json:"redirectURL"`
State string `json:"state"`
}
newAuthUrlRequest := r{}
if err := json.Unmarshal(msg.Data, &newAuthUrlRequest); err != nil {
return ctx.Send(new(HandlerResponse).SetError(err.Error()).MustMarshall(), CodeErrIncorrectParams)
}

redirectURL := newAuthUrlRequest.RedirectURL
provider, ok := providers[requestedProvider]
if !ok {
return ctx.Send(new(HandlerResponse).SetError(ReasonErrOauthProviderNotFound).MustMarshall(), CodeErrOauthProviderNotFound)
Expand All @@ -170,7 +170,7 @@ func (f *faucet) authOAuthUrl(msg *apirest.APIdata, ctx *httprouter.HTTPContext)
type urlResponse struct {
Url string `json:"url"`
}
authURL := urlResponse{Url: provider.GetAuthURL(redirectURL)}
authURL := urlResponse{Url: provider.GetAuthURL(newAuthUrlRequest.RedirectURL, newAuthUrlRequest.State)}
return ctx.Send(new(HandlerResponse).Set(authURL).MustMarshall(), apirest.HTTPstatusOK)
}

Expand Down
13 changes: 10 additions & 3 deletions oauthhandler/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ func InitProviders() (map[string]*Provider, error) {
}

// GetAuthURL returns the OAuth authorize URL for the provider.
func (p *Provider) GetAuthURL(redirectURL string) string {
func (p *Provider) GetAuthURL(redirectURL string, state string) string {
u, _ := url.Parse(p.AuthURL)
q := u.Query()
q.Set("client_id", p.ClientID)
q.Set("redirect_uri", redirectURL)
q.Set("scope", p.Scope)
q.Set("response_type", "token")
q.Set("response_type", "code")
q.Set("state", state)
u.RawQuery = q.Encode()
return u.String()
}
Expand All @@ -118,7 +119,12 @@ func (p *Provider) GetOAuthToken(code string, redirectURL string) (*OAuthToken,
data.Set("client_id", p.ClientID)
data.Set("client_secret", p.ClientSecret)
data.Set("redirect_uri", redirectURL)
data.Set("code", code)

unescapedCode, err := url.QueryUnescape(code)
if err != nil {
return nil, err
}
data.Set("code", unescapedCode)

req, err := http.NewRequest("POST", p.TokenURL, strings.NewReader(data.Encode()))
if err != nil {
Expand All @@ -143,6 +149,7 @@ func (p *Provider) GetOAuthToken(code string, redirectURL string) (*OAuthToken,
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Warnw("failed to get OAuth token", "body", string(body))
return nil, fmt.Errorf("failed to get OAuth token: %s", body)
}

Expand Down

0 comments on commit 3334e59

Please sign in to comment.