Skip to content

Commit

Permalink
Amend auth to add the domain as part of the set of results
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Jan 23, 2021
1 parent a13b6c4 commit ccd9d87
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Authenticator struct {

type Result struct {
Tenant string
Domain string
AccessToken string
ExpiresIn int64
}
Expand Down Expand Up @@ -106,14 +107,15 @@ func (a *Authenticator) Wait(ctx context.Context, state State) (Result, error) {
return Result{}, errors.New(res.ErrorDescription)
}

t, err := parseTenant(res.AccessToken)
ten, domain, err := parseTenant(res.AccessToken)
if err != nil {
return Result{}, fmt.Errorf("cannot parse tenant from the given access token: %w", err)
}
return Result{
AccessToken: res.AccessToken,
ExpiresIn: res.ExpiresIn,
Tenant: t,
Tenant: ten,
Domain: domain,
}, nil
}
}
Expand All @@ -139,27 +141,27 @@ func (a *Authenticator) getDeviceCode(ctx context.Context) (State, error) {
return res, nil
}

func parseTenant(accessToken string) (string, error) {
func parseTenant(accessToken string) (tenant, domain string, err error) {
parts := strings.Split(accessToken, ".")
v, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return "", err
return "", "", err
}
var payload struct {
AUDs []string `json:"aud"`
}
if err := json.Unmarshal([]byte(v), &payload); err != nil {
return "", err
return "", "", err
}
for _, aud := range payload.AUDs {
u, err := url.Parse(aud)
if err != nil {
return "", err
return "", "", err
}
if u.Path == audiencePath {
parts := strings.Split(u.Host, ".")
return parts[0], nil
return parts[0], u.Host, nil
}
}
return "", fmt.Errorf("audience not found for %s", audiencePath)
return "", "", fmt.Errorf("audience not found for %s", audiencePath)
}

0 comments on commit ccd9d87

Please sign in to comment.