Skip to content

Commit 80b1f35

Browse files
handsomejack-42bgavrilMS
authored andcommitted
refactor(oauth): make WithTenant extensible with authority types
1 parent 328056f commit 80b1f35

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

apps/internal/oauth/ops/authority/authority.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,23 +235,24 @@ func NewAuthParams(clientID string, authorityInfo Info) AuthParams {
235235
// - the client is configured to authenticate only Microsoft accounts via the "consumers" endpoint
236236
// - the resulting authority URL is invalid
237237
func (p AuthParams) WithTenant(ID string) (AuthParams, error) {
238-
switch ID {
239-
case "", p.AuthorityInfo.Tenant:
240-
// keep the default tenant because the caller didn't override it
238+
if ID == "" || ID == p.AuthorityInfo.Tenant {
241239
return p, nil
242-
case "common", "consumers", "organizations":
243-
if p.AuthorityInfo.AuthorityType == AAD {
240+
}
241+
242+
var authority string
243+
switch p.AuthorityInfo.AuthorityType {
244+
case AAD:
245+
if ID == "common" || ID == "consumers" || ID == "organizations" {
244246
return p, fmt.Errorf(`tenant ID must be a specific tenant, not "%s"`, ID)
245247
}
246-
// else we'll return a better error below
247-
}
248-
if p.AuthorityInfo.AuthorityType != AAD {
249-
return p, errors.New("the authority doesn't support tenants")
250-
}
251-
if p.AuthorityInfo.Tenant == "consumers" {
252-
return p, errors.New(`client is configured to authenticate only personal Microsoft accounts, via the "consumers" endpoint`)
248+
if p.AuthorityInfo.Tenant == "consumers" {
249+
return p, errors.New(`client is configured to authenticate only personal Microsoft accounts, via the "consumers" endpoint`)
250+
}
251+
authority = "https://" + path.Join(p.AuthorityInfo.Host, ID)
252+
case ADFS:
253+
return p, errors.New("ADFS authority doesn't support tenants")
253254
}
254-
authority := "https://" + path.Join(p.AuthorityInfo.Host, ID)
255+
255256
info, err := NewInfoFromAuthorityURI(authority, p.AuthorityInfo.ValidateAuthority, p.AuthorityInfo.InstanceDiscoveryDisabled)
256257
if err == nil {
257258
info.Region = p.AuthorityInfo.Region

0 commit comments

Comments
 (0)