Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift 3,4,5...) Get an Azure AD Access TokenDemonstrates how to obtain an Azure AD access token for authentication using a client ID, client secret, and tenant ID.
func chilkatTest() { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. let socket = CkoSocket()! // Make a TLS connection to login.microsoftonline.com, waiting at most 5000 milliseconds. var success: Bool = socket.connect("login.microsoftonline.com", port: 443, ssl: true, maxWaitMs: 5000) if success != true { print("\(socket.lastErrorText!)") return } // Create an Azure AD auth object, and provide the required information for authorization. let azureAD = CkoAuthAzureAD()! azureAD.clientId = "AZURE_AD_CLIENT_ID" azureAD.clientSecret = "AZURE_AD_CLIENT_SECRET" azureAD.tenantId = "AZURE_TENANT_ID" azureAD.resource = "https://outlook.office365.com/" // Retrieve the access token using the TLS connection to login.microsoftonline.com success = azureAD.obtainAccessToken(socket) if success != true { print("\(socket.lastErrorText!)") return } // Show the access token, and then save it to a JSON file // for future use (such as with a REST method call). print("Azure AD Access Token = \(azureAD.accessToken!)") let json = CkoJsonObject()! json.append("accessToken", value: azureAD.accessToken) // Save our access token to a file. It will be used in subsequent REST API calls. let fac = CkoFileAccess()! fac.writeEntireTextFile("qa_data/tokens/azureAD_office365.json", fileData: json.emit(), charset: "utf-8", includePreamble: false) // Let's get the id information out of the access token. // Our JSON looks like this: // {"accessToken":"eyJ0eXAiO---TdjT3RjIn0.eyJhdWQiOiJo---jEuMCJ9.CIx0sUT8s---KvzKKUw"} // I used "---" instead of "..." to indicate a large quantity of omitted chars. // The accessToken is a long string composed of 3 base64 strings concatenated with "." chars. // The 1st part is the JOSE header. If you decode from base64, you'll get the JSON JOSE header. // The 2nd part is the id token. When decoded this is the JSON that contains information about the authenticated application. // The 3rd part is binary and does not decode to JSON. // Let's get the information from the 2nd part (the id token) let sa = CkoStringArray()! sa.splitAndAppend(json.string(of: "accessToken"), boundary: ".") let sbIdToken = CkoStringBuilder()! // The 2nd string is at index 1. sbIdToken.append(sa.getString(1)) sbIdToken.decode("base64", charset: "utf-8") let jsonIdToken = CkoJsonObject()! jsonIdToken.loadSb(sbIdToken) jsonIdToken.emitCompact = false print("\(jsonIdToken.emit()!)") // We have something like this: // { // "aud": "https://outlook.office365.com/", // "iss": "https://sts.windows.net/6e8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/", // "iat": 1586350465, // "nbf": 1586350465, // "exp": 1586354365, // "aio": "42dgYNjyOtX8ZZB7JLfMFJGeKUmvAA==", // "app_displayname": "ChilkatTest", // "appid": "f125d695-c50e-456e-a578-a486f06d1213", // "appidacr": "1", // "idp": "https://sts.windows.net/6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/", // "oid": "7545a2fd-3f0d-48a4-9c58-a1a5700a24b1", // "sid": "ab981252-9378-4d0c-964b-eb2e1451138f", // "sub": "7546a2fc-3f0d-48a4-9c58-a1a5700a24b1", // "tid": "6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd", // "uti": "huIJBAa1tvGpczwV5S1BAA", // "ver": "1.0" // } // Get the desired information from the JSON: var aud: String? = jsonIdToken.string(of: "aud") var iss: String? = jsonIdToken.string(of: "iss") var iat: Int = jsonIdToken.int(of: "iat").intValue var nbf: Int = jsonIdToken.int(of: "nbf").intValue var exp: Int = jsonIdToken.int(of: "exp").intValue var aio: String? = jsonIdToken.string(of: "aio") var app_displayname: String? = jsonIdToken.string(of: "app_displayname") var appid: String? = jsonIdToken.string(of: "appid") var appidacr: String? = jsonIdToken.string(of: "appidacr") var idp: String? = jsonIdToken.string(of: "idp") var oid: String? = jsonIdToken.string(of: "oid") var sid: String? = jsonIdToken.string(of: "sid") var s_sub: String? = jsonIdToken.string(of: "sub") var tid: String? = jsonIdToken.string(of: "tid") var uti: String? = jsonIdToken.string(of: "uti") var ver: String? = jsonIdToken.string(of: "ver") } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.