2022-05-12 11:17:24 +02:00
|
|
|
package internal
|
2022-03-20 08:29:18 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
// OAuthClient is a OAuth client interface for various idp providers
|
|
|
|
type OAuthClient interface {
|
|
|
|
RequestDeviceCode(ctx context.Context) (DeviceAuthInfo, error)
|
|
|
|
RotateAccessToken(ctx context.Context, refreshToken string) (TokenInfo, error)
|
|
|
|
WaitToken(ctx context.Context, info DeviceAuthInfo) (TokenInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPClient http client interface for API calls
|
|
|
|
type HTTPClient interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeviceAuthInfo holds information for the OAuth device login flow
|
|
|
|
type DeviceAuthInfo struct {
|
|
|
|
DeviceCode string `json:"device_code"`
|
|
|
|
UserCode string `json:"user_code"`
|
|
|
|
VerificationURI string `json:"verification_uri"`
|
|
|
|
VerificationURIComplete string `json:"verification_uri_complete"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
Interval int `json:"interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenInfo holds information of issued access token
|
|
|
|
type TokenInfo struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
IDToken string `json:"id_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// HostedGrantType grant type for device flow on Hosted
|
2022-03-22 13:12:11 +01:00
|
|
|
const (
|
2022-05-12 11:17:24 +02:00
|
|
|
HostedGrantType = "urn:ietf:params:oauth:grant-type:device_code"
|
|
|
|
HostedRefreshGrant = "refresh_token"
|
2022-03-22 13:12:11 +01:00
|
|
|
)
|
2022-03-20 08:29:18 +01:00
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
// Hosted client
|
|
|
|
type Hosted struct {
|
|
|
|
// Hosted API Audience for validation
|
2022-03-20 08:29:18 +01:00
|
|
|
Audience string
|
2022-05-12 11:17:24 +02:00
|
|
|
// Hosted Native application client id
|
2022-03-20 08:29:18 +01:00
|
|
|
ClientID string
|
2022-05-12 11:17:24 +02:00
|
|
|
// Hosted domain
|
2022-03-20 08:29:18 +01:00
|
|
|
Domain string
|
|
|
|
|
|
|
|
HTTPClient HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestDeviceCodePayload used for request device code payload for auth0
|
|
|
|
type RequestDeviceCodePayload struct {
|
|
|
|
Audience string `json:"audience"`
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenRequestPayload used for requesting the auth0 token
|
|
|
|
type TokenRequestPayload struct {
|
2022-03-22 13:12:11 +01:00
|
|
|
GrantType string `json:"grant_type"`
|
|
|
|
DeviceCode string `json:"device_code,omitempty"`
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
RefreshToken string `json:"refresh_token,omitempty"`
|
2022-03-20 08:29:18 +01:00
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
// TokenRequestResponse used for parsing Hosted token's response
|
2022-03-20 08:29:18 +01:00
|
|
|
type TokenRequestResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
ErrorDescription string `json:"error_description"`
|
|
|
|
TokenInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Claims used when validating the access token
|
|
|
|
type Claims struct {
|
2022-03-22 13:12:11 +01:00
|
|
|
Audience string `json:"aud"`
|
2022-03-20 08:29:18 +01:00
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
// NewHostedDeviceFlow returns an Hosted OAuth client
|
|
|
|
func NewHostedDeviceFlow(audience string, clientID string, domain string) *Hosted {
|
2022-03-20 08:29:18 +01:00
|
|
|
httpTransport := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
httpTransport.MaxIdleConns = 5
|
|
|
|
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
Transport: httpTransport,
|
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
return &Hosted{
|
2022-03-20 08:29:18 +01:00
|
|
|
Audience: audience,
|
|
|
|
ClientID: clientID,
|
|
|
|
Domain: domain,
|
|
|
|
HTTPClient: httpClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
// RequestDeviceCode requests a device code login flow information from Hosted
|
|
|
|
func (h *Hosted) RequestDeviceCode(ctx context.Context) (DeviceAuthInfo, error) {
|
|
|
|
url := "https://" + h.Domain + "/oauth/device/code"
|
2022-03-20 08:29:18 +01:00
|
|
|
codePayload := RequestDeviceCodePayload{
|
2022-05-12 11:17:24 +02:00
|
|
|
Audience: h.Audience,
|
|
|
|
ClientID: h.ClientID,
|
2022-03-20 08:29:18 +01:00
|
|
|
}
|
|
|
|
p, err := json.Marshal(codePayload)
|
|
|
|
if err != nil {
|
|
|
|
return DeviceAuthInfo{}, fmt.Errorf("parsing payload failed with error: %v", err)
|
|
|
|
}
|
|
|
|
payload := strings.NewReader(string(p))
|
|
|
|
req, err := http.NewRequest("POST", url, payload)
|
|
|
|
if err != nil {
|
|
|
|
return DeviceAuthInfo{}, fmt.Errorf("creating request failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
res, err := h.HTTPClient.Do(req)
|
2022-03-20 08:29:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return DeviceAuthInfo{}, fmt.Errorf("doing request failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return DeviceAuthInfo{}, fmt.Errorf("reading body failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return DeviceAuthInfo{}, fmt.Errorf("request device code returned status %d error: %s", res.StatusCode, string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceCode := DeviceAuthInfo{}
|
|
|
|
err = json.Unmarshal(body, &deviceCode)
|
|
|
|
if err != nil {
|
|
|
|
return DeviceAuthInfo{}, fmt.Errorf("unmarshaling response failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return deviceCode, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitToken waits user's login and authorize the app. Once the user's authorize
|
2022-05-12 11:17:24 +02:00
|
|
|
// it retrieves the access token from Hosted's endpoint and validates it before returning
|
|
|
|
func (h *Hosted) WaitToken(ctx context.Context, info DeviceAuthInfo) (TokenInfo, error) {
|
2022-03-20 08:29:18 +01:00
|
|
|
ticker := time.NewTicker(time.Duration(info.Interval) * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return TokenInfo{}, ctx.Err()
|
|
|
|
case <-ticker.C:
|
2022-05-12 11:17:24 +02:00
|
|
|
url := "https://" + h.Domain + "/oauth/token"
|
2022-03-20 08:29:18 +01:00
|
|
|
tokenReqPayload := TokenRequestPayload{
|
2022-05-12 11:17:24 +02:00
|
|
|
GrantType: HostedGrantType,
|
2022-03-20 08:29:18 +01:00
|
|
|
DeviceCode: info.DeviceCode,
|
2022-05-12 11:17:24 +02:00
|
|
|
ClientID: h.ClientID,
|
2022-03-20 08:29:18 +01:00
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
body, statusCode, err := requestToken(h.HTTPClient, url, tokenReqPayload)
|
2022-03-20 08:29:18 +01:00
|
|
|
if err != nil {
|
2022-03-22 13:12:11 +01:00
|
|
|
return TokenInfo{}, fmt.Errorf("wait for token: %v", err)
|
2022-03-20 08:29:18 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 13:12:11 +01:00
|
|
|
if statusCode > 499 {
|
2022-03-20 08:29:18 +01:00
|
|
|
return TokenInfo{}, fmt.Errorf("wait token code returned error: %s", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenResponse := TokenRequestResponse{}
|
|
|
|
err = json.Unmarshal(body, &tokenResponse)
|
|
|
|
if err != nil {
|
|
|
|
return TokenInfo{}, fmt.Errorf("parsing token response failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokenResponse.Error != "" {
|
|
|
|
if tokenResponse.Error == "authorization_pending" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return TokenInfo{}, fmt.Errorf(tokenResponse.ErrorDescription)
|
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
err = isValidAccessToken(tokenResponse.AccessToken, h.Audience)
|
2022-03-20 08:29:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return TokenInfo{}, fmt.Errorf("validate access token failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenInfo := TokenInfo{
|
|
|
|
AccessToken: tokenResponse.AccessToken,
|
|
|
|
TokenType: tokenResponse.TokenType,
|
|
|
|
RefreshToken: tokenResponse.RefreshToken,
|
|
|
|
IDToken: tokenResponse.IDToken,
|
|
|
|
ExpiresIn: tokenResponse.ExpiresIn,
|
|
|
|
}
|
|
|
|
return tokenInfo, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 13:12:11 +01:00
|
|
|
// RotateAccessToken requests a new token using an existing refresh token
|
2022-05-12 11:17:24 +02:00
|
|
|
func (h *Hosted) RotateAccessToken(ctx context.Context, refreshToken string) (TokenInfo, error) {
|
|
|
|
url := "https://" + h.Domain + "/oauth/token"
|
2022-03-22 13:12:11 +01:00
|
|
|
tokenReqPayload := TokenRequestPayload{
|
2022-05-12 11:17:24 +02:00
|
|
|
GrantType: HostedRefreshGrant,
|
|
|
|
ClientID: h.ClientID,
|
2022-03-22 13:12:11 +01:00
|
|
|
RefreshToken: refreshToken,
|
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
body, statusCode, err := requestToken(h.HTTPClient, url, tokenReqPayload)
|
2022-03-22 13:12:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return TokenInfo{}, fmt.Errorf("rotate access token: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusCode != 200 {
|
|
|
|
return TokenInfo{}, fmt.Errorf("rotating token returned error: %s", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenResponse := TokenRequestResponse{}
|
|
|
|
err = json.Unmarshal(body, &tokenResponse)
|
|
|
|
if err != nil {
|
|
|
|
return TokenInfo{}, fmt.Errorf("parsing token response failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
err = isValidAccessToken(tokenResponse.AccessToken, h.Audience)
|
2022-03-22 13:12:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return TokenInfo{}, fmt.Errorf("validate access token failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenInfo := TokenInfo{
|
|
|
|
AccessToken: tokenResponse.AccessToken,
|
|
|
|
TokenType: tokenResponse.TokenType,
|
|
|
|
RefreshToken: tokenResponse.RefreshToken,
|
|
|
|
IDToken: tokenResponse.IDToken,
|
|
|
|
ExpiresIn: tokenResponse.ExpiresIn,
|
|
|
|
}
|
|
|
|
return tokenInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func requestToken(client HTTPClient, url string, tokenReqPayload TokenRequestPayload) ([]byte, int, error) {
|
|
|
|
p, err := json.Marshal(tokenReqPayload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("parsing token payload failed with error: %v", err)
|
|
|
|
}
|
|
|
|
payload := strings.NewReader(string(p))
|
|
|
|
req, err := http.NewRequest("POST", url, payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("creating token request failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("doing token request failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("reading token body failed with error: %v", err)
|
|
|
|
}
|
|
|
|
return body, res.StatusCode, nil
|
|
|
|
}
|
|
|
|
|
2022-03-20 08:29:18 +01:00
|
|
|
// isValidAccessToken is a simple validation of the access token
|
|
|
|
func isValidAccessToken(token string, audience string) error {
|
|
|
|
if token == "" {
|
|
|
|
return fmt.Errorf("token received is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedClaims := strings.Split(token, ".")[1]
|
|
|
|
claimsString, err := base64.RawURLEncoding.DecodeString(encodedClaims)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := Claims{}
|
|
|
|
err = json.Unmarshal(claimsString, &claims)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-22 13:12:11 +01:00
|
|
|
if claims.Audience != audience {
|
2022-03-20 08:29:18 +01:00
|
|
|
return fmt.Errorf("invalid audience")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|