Invalid base64 saml assertion when obtained in OAuth on-behalf-of flow

olifantastic 26 Reputation points
2021-02-22T09:51:24.797+00:00

Summary
My task is to receive a valid SAML Assertion from an existing OAuth access token on Azure AD. The purpose of this is, to access a SAML protected API using the users credentials. Changing the API to OAuth is not an option.

Description
Following the documentation for Azure AD Api V1 (v1-oauth2-on-behalf-of-flow.md), I was able to get a SAML Assertion. I requested the assertion also following:

  • grant_type: urn:ietf:params:oauth:grant-type:jwt-bearer
  • assertion: <access-token-from-user-requesting-access-to-saml-api>
  • client_id: <client-id-which-requests-resource>
  • client_secret: <assigned-secret>
  • resource: <resource-which-accept-saml-assertion>
  • requested_token_use: on_behalf_of
  • requested_token_type: urn:ietf:params:oauth:token-type:saml2

In addition to that I added x-www-form-urlencoded as content type. This request is sent to "https://login.microsoftonline.com/<tenant-id>/oauth2/token" as HTTP POST. The setup I have is:

  • A React Front-End which authenticates the user on Azure AD using OAuth.
  • A Back-End for the Front-End, which requests the SAML Assertion on-beahlf-of the user (performing the above described request).
  • An API exposing data which the Front-End displays.

Problem

I receive a valid response from Azure AD containing all the necessary fields, which looks like this:

"token_type": "Bearer",
"expires_in": "3579",
"ext_expires_in": "3579",
"expires_on": "1613985579",
"resource": "spn:saml-test",
"access_token": "PEFzc2Vyd...9uPg",
"issued_token_type": "urn:ietf:params:oauth:token-type:saml2",
"refresh_token": "0.ATEAt...SJto5hclkg-7g"

The received assertion in the access_token field is not a valid base64 string (which it should be IMO). Feeding this string to the C# Base64Convert function, results in an exception:

System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
   at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
   at System.Convert.FromBase64String(String s)
   at Validator.Program.Main(String[] args) 

I was however able to decode it using base64 -D on my terminal, which gave me a almost valid SAML Assertion, however it also complained about invalid characters in the string. Actually the only wrong character was the last closing bracket (>) in the XML document. Correcting that manually and converting it into a base64 string to compare it with the received assertion showed me the following errors in the initially received assertion:

  • At the end of the string should be two equal signs
  • All - characters should be + characters

Furthermore, the received SAML Assertion is also not accepted by the SAML API, causing a similar exception.

Question
Am I doing something wrong to obtain the Assertion? Or is this a known issue with V1 OBO Flow? I prefer not to modify the received Assertion, and just to pass it to the SAML API. Is there a known workaround for this?

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,629 questions
{count} vote

Accepted answer
  1. Siva-kumar-selvaraj 15,606 Reputation points
    2021-02-26T13:32:47.693+00:00

    Hello @olifantastic ,

    Thanks for reaching out.

    I don’t see anything wrong with above steps that you are referring, I had followed the same steps to test this OAuth on-behalf-of flow through Postman tool and was able to get success assertion as shown below, with that I had used Fiddler’s TextWizard feature (Transform = From Base64) (see below screenshot for your reference) which decoded token just fine and the tool available at https://www.freeformatter.com/base64-encoder.html.

    I had found below alternate way while doing further research (I wasn't tested this one on my lab), but am yet to get more insight on this from our product team, meanwhile I would recommend you to try either of way and see if that works for your environment, fiddler way or Online tool or below workaround.

    Workaround:
    Doing this in C# code may not work: byte[] data = Convert.FromBase64(customBase64);
    Alternatively. try to use the HttpServerUtility.UrlTokenDecode method, it expects that at the end of the data it contains the number of paddings.
    The right code:
    if (base64String.Length % 4 != 0) base64String += (4 – base64String.Length % 4);
    else base64String += 0;
    var data = Encoding.UTF8.GetString(HttpServerUtility.UrlTokenDecode(base64String));

    Hope this helps.

    Request:

    72427-image.png

    Response:

    {  
        "token_type": "Bearer",  
        "expires_in": "3455",  
        "ext_expires_in": "3455",  
        "expires_on": "1614346716",  
        "resource": "api://cdb02891-7976-45b0-91e2-d71157c4d2e0",  
        "access_token": "PEFzc2VydGlvbiBJRD0iXzQzNWU1MWRjLWYxM2YtNDRkNi04MjA3LWU3ZWUwOTM2MTcwMCIgSXNzdWVJbnN0YW50PSIyMDIxLTAyLTI2VDEyOjQxOjAwLjUwNFoiIFZlcnNpb249IjIuMCIgeG1sbnM9InVybjpvYXNpczpuYW1lczp0Yzp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3NlczpQYXNzd29yZDwvQXV0aG5Db250ZXh0Q2xhc3NSZWY-PC9BdXRobkNvbnRleHQ-PC9BdXRoblN0YXRlbWVudD48L0Fzc2VydGlvbj4",  
        "issued_token_type": "urn:ietf:params:oauth:token-type:saml2",  
    

    }

    Fiddler:
    72515-image.png

    ------------------------------------------------------------------------------------------------------------------------------

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


0 additional answers

Sort by: Most helpful