Codeunit 2361 HttpAuthOAuthClientCredentials in 24
- App
- System Application
- Namespace
- System.RestClient
Versions171819202122232425262728latest
Source in 24
src/System Application/App/Rest Client/src/Authentication/HttpAuthOAuthClientCredentials.Codeunit.al78 lines, Copyright (c) Microsoft Corporation. MIT
/// <summary>Implementation of the "Http Authentication" interface for a request that requires basic authentication</summary>
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.RestClient;
codeunit 2361 "HttpAuthOAuthClientCredentials" implements "Http Authentication"
{
InherentEntitlements = X;
InherentPermissions = X;
var
BearerTxt: Label 'Bearer %1', Comment = '%1 - Token', Locked = true;
ScopesGlobal: List of [Text];
ClientSecretGlobal: SecretText;
ClientIdGlobal: Text;
OAuthAuthorityUrlGlobal: Text;
/// <summary>
/// Initializes the authentication object with the given AuthorityUrl, ClientId, ClientSecret and scopes
/// </summary>
/// <param name="OAuthAuthorityUrl">The OAuthAuthorityUrl to use for authentication</param>
/// <param name="ClientId">The ClientId to use for authentication</param>
/// <param name="ClientSecret">The ClientSecret to use for authentication</param>
/// <param name="Scopes">The Scopes to use for authentication</param>
procedure Initialize(OAuthAuthorityUrl: Text; ClientId: Text; ClientSecret: SecretText; Scopes: List of [Text])
begin
OAuthAuthorityUrlGlobal := OAuthAuthorityUrl;
ClientIdGlobal := ClientId;
ClientSecretGlobal := ClientSecret;
ScopesGlobal := Scopes;
end;
/// <summary>Checks if authentication is required for the request</summary>
/// <returns>Returns true because authentication is required</returns>
procedure IsAuthenticationRequired(): Boolean;
begin
exit(true);
end;
/// <summary>Gets the authorization headers for the request</summary>
/// <returns>Returns a dictionary of headers that need to be added to the request</returns>
procedure GetAuthorizationHeaders() Header: Dictionary of [Text, SecretText];
begin
Header.Add('Authorization', SecretStrSubstNo(BearerTxt, GetToken()));
end;
local procedure GetToken(): SecretText
var
AccessToken: SecretText;
ErrorText: Text;
begin
if not AcquireToken(AccessToken, ErrorText) then
Error(ErrorText);
exit(AccessToken);
end;
local procedure AcquireToken(var AccessToken: SecretText; var ErrorText: Text): Boolean
var
OAuth2: Codeunit System.Security.Authentication.OAuth2;
IsSuccess: Boolean;
AquireTokenFailedErr: Label 'Acquire of token with Client Credentials failed.';
begin
if (not OAuth2.AcquireAuthorizationCodeTokenFromCache(ClientIdGlobal, ClientSecretGlobal, '', OAuthAuthorityUrlGlobal, ScopesGlobal, AccessToken) or (AccessToken.IsEmpty())) then
OAuth2.AcquireTokenWithClientCredentials(ClientIdGlobal, ClientSecretGlobal, OAuthAuthorityUrlGlobal, '', ScopesGlobal, AccessToken);
IsSuccess := not AccessToken.IsEmpty();
if not IsSuccess then begin
ErrorText := GetLastErrorText();
if ErrorText = '' then
ErrorText := AquireTokenFailedErr;
end;
exit(IsSuccess);
end;
}Procedures, 3
| Name | Parameters | Returns | Access | Obsolete |
|---|---|---|---|---|
| Initialize | (Text, Text, SecretText, List) | public | - | |
| IsAuthenticationRequired | () | Boolean | public | - |
| GetAuthorizationHeaders | () | Dictionary | public | - |