Sample code for 30+ languages & platforms
PureBasic

Decode Microsoft Graph ID Token

See more Microsoft Graph Examples

Demonstrates how to decode a Microsoft Graph ID token.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJwt.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; In a previous example, we obtained a Microsoft Graph OAuth2 access token.
    ; This example loads the JSON saved from the previous example and decodes the id_token.

    ; Our Microsoft Graph OAuth2 token looks like this:

    ; {
    ;   "token_type": "Bearer",
    ;   "scope": "openid profile User.ReadWrite Mail.ReadWrite Mail.Send Files.ReadWrite User.Read Calendars.ReadWrite Group.ReadWrite.All",
    ;   "expires_in": 3600,
    ;   "ext_expires_in": 3600,
    ;   "access_token": "EwCQA8l6...0HhMKYwC",
    ;   "refresh_token": "MCWulIvzi2yD0S...igEFn51mqcByhZtAJg",
    ;   "id_token": "eyJ0eXAiOiJKV1...Q7lRDaR-7A",
    ;   "expires_on": "1562862714"
    ; }

    jsonToken.i = CkJsonObject::ckCreate()
    If jsonToken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/microsoftGraph.json")
    If success = 0
        Debug "Failed to load the JSON file..."
        CkJsonObject::ckDispose(jsonToken)
        ProcedureReturn
    EndIf

    ; Use Chilkat's JWT API to examine the id_token..
    jwt.i = CkJwt::ckCreate()
    If jwt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    idToken.s = CkJsonObject::ckStringOf(jsonToken,"id_token")

    ; Extract the JOSE header..
    jose.s = CkJwt::ckGetHeader(jwt,idToken)

    jsonHeader.i = CkJsonObject::ckCreate()
    If jsonHeader.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonHeader,jose)
    CkJsonObject::setCkEmitCompact(jsonHeader, 0)
    Debug CkJsonObject::ckEmit(jsonHeader)

    ; The JOSE header looks like this:

    ; {
    ;   "typ": "JWT",
    ;   "alg": "RS256",
    ;   "kid": "1LTMzakihiRla_8z2BEJVXeWMqo"
    ; }

    claims.s = CkJwt::ckGetPayload(jwt,idToken)

    jsonClaims.i = CkJsonObject::ckCreate()
    If jsonClaims.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonClaims,claims)
    CkJsonObject::setCkEmitCompact(jsonClaims, 0)
    Debug CkJsonObject::ckEmit(jsonClaims)

    ; The claims look like this:

    ; {
    ;   "ver": "2.0",
    ;   "iss": "https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0",
    ;   "sub": "AAAAAAAAAAAAAAAAAAAAAHJFryd6Gydo-XtTd1nhUNQ",
    ;   "aud": "18c456bd-db75-43fe-9724-9e5d821c68ff",
    ;   "exp": 1562945513,
    ;   "iat": 1562858813,
    ;   "nbf": 1562858813,
    ;   "name": "Matt Chilkat",
    ;   "preferred_username": "matt@example.com",
    ;   "oid": "00000000-0000-0000-3a33-fceb9b74cc15",
    ;   "tid": "9188040d-6c67-4c5b-b112-36a304b66dad",
    ;   "aio": "DfibJqKnWC1c0FS6G ... W6pvTrQuYzyq16ghY$"
    ; }
    ; 

    ; Use this online tool to generate parsing code from sample JSON: 
    ; Generate Parsing Code from JSON

    ; Get each of the claims..
    ver.s = CkJsonObject::ckStringOf(jsonClaims,"ver")
    iss.s = CkJsonObject::ckStringOf(jsonClaims,"iss")
    s_sub.s = CkJsonObject::ckStringOf(jsonClaims,"sub")
    aud.s = CkJsonObject::ckStringOf(jsonClaims,"aud")
    exp.i = CkJsonObject::ckIntOf(jsonClaims,"exp")
    iat.i = CkJsonObject::ckIntOf(jsonClaims,"iat")
    nbf.i = CkJsonObject::ckIntOf(jsonClaims,"nbf")
    name.s = CkJsonObject::ckStringOf(jsonClaims,"name")
    preferred_username.s = CkJsonObject::ckStringOf(jsonClaims,"preferred_username")
    oid.s = CkJsonObject::ckStringOf(jsonClaims,"oid")
    tid.s = CkJsonObject::ckStringOf(jsonClaims,"tid")
    aio.s = CkJsonObject::ckStringOf(jsonClaims,"aio")


    CkJsonObject::ckDispose(jsonToken)
    CkJwt::ckDispose(jwt)
    CkJsonObject::ckDispose(jsonHeader)
    CkJsonObject::ckDispose(jsonClaims)


    ProcedureReturn
EndProcedure