Ayush Singh
Ayush Singh

Reputation: 51

Getting access token for an api protected by B2C, using custom policies

I have an api that is protected using ADB2C authentication. I need to call this api via custom policies. I followed the documentation enter link description here and have added the two technical profiles as validation technical profile of a self asserted profile.

I am getting an access token returned by the below technical profile :

<TechnicalProfile Id="SecureREST-AccessToken">
      <DisplayName></DisplayName>
      <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <Metadata>
        <Item Key="ServiceUrl">https://login.microsoftonline.com/{tenant id here}/oauth2/v2.0/token</Item>
        <Item Key="AuthenticationType">Basic</Item>
        <Item Key="SendClaimsIn">Form</Item>
      </Metadata>
      <CryptographicKeys>
        <Key Id="BasicAuthenticationUsername" StorageReferenceId="B2C_1A_SecureRESTClientId" />
        <Key Id="BasicAuthenticationPassword" StorageReferenceId="B2C_1A_SecureRESTClientSecret" />
      </CryptographicKeys>
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="grant_type" DefaultValue="client_credentials" />
        <InputClaim ClaimTypeReferenceId="scope" DefaultValue="{app id uri for protected resource}/.default" />
      </InputClaims>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="bearerToken" PartnerClaimType="access_token" />
      </OutputClaims>
      <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
    </TechnicalProfile>

And then making the rest api call using below profile :

<TechnicalProfile Id="UserMigrationViaLegacyIdp">
                <DisplayName>REST API call to communicate with Legacy IdP</DisplayName>
                <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
                <Metadata>
                    <Item Key="ServiceUrl">
         
          https://99a0a14a6402.ngrok.io/api/Identity/SignUpAsync
        </Item>
                    <Item Key="AuthenticationType">Bearer</Item>
                    <Item Key="SendClaimsIn">Header</Item>
                    <Item Key="AllowInsecureAuthInProduction">false</Item>
        <Item Key="UseClaimAsBearerToken">bearerToken</Item>


      </Metadata>
                <InputClaims>
                    <InputClaim ClaimTypeReferenceId="bearerToken"/>
      </InputClaims>
                <OutputClaims>
                    
                    <OutputClaim ClaimTypeReferenceId="phonePresent"/>

                </OutputClaims>
                <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
            </TechnicalProfile>

However, scopes are missing from the returned access token, hence token validation is failing on the api.

Is my call to get access token missing anything?

Upvotes: 1

Views: 1407

Answers (1)

Chris Padgett
Chris Padgett

Reputation: 14634

For the client credentials grant flow, the API permissions must be created as roles (see How to: Add app roles to your application and receive them in the token) and then granted admin consent (see Admin consent button).

As result, the bearer token contains the roles claim, rather than the scp claim.

The API application checks access using this roles claim (see Verify app roles in APIs called by daemon apps).

Upvotes: 1

Related Questions