Árvore de páginas


01. DADOS GERAIS

Produto:

TOTVS Logística Recintos Aduaneiros

Linha de Produto:

Linha Logix

Segmento:

Logística

Módulo:Coletor de dados mobile
Função:Autenticação OAuth2
Ticket:
Requisito/Story/Issue (informe o requisito relacionado) :
  1. DLOGPORTOS-17325
  2. DLOGPORTOS-17639


02. SITUAÇÃO/REQUISITO

Para atendimento da demanda de autenticação, para o produto TOTVS Recintos Aduaneiros, através do protocolo OAuth2 foi estabelecida a componentização da classe de login a ser realizado na aplicação, através da implementação da interface IOAuth2Authenticator.

03. SOLUÇÃO

A implementação da interface depende dos seguintes elementos do framework de desenvolvimento:

    • Framework.Consts.HTTPCodes;
    • Framework.Consts.OAuth2;
    • Framework.Helpers.DateTime;
    • Framework.Helpers.JSON;
    • Framework.Helpers.Str e;
    • Framework.Types.ContentType.


São métodos expostos pela implementação da interface IOAuth2Authenticator:

  • class function Instance: IOAuth2Authenticator;
  • function GetAuthToken(out AToken: string): IOAuth2Authenticator;
  • function SetAcrValues(const Value: string): IOAuth2Authenticator;
  • function SetClientID(const Value: string): IOAuth2Authenticator;
  • function SetClientSecret(const Value: string): IOAuth2Authenticator;
  • function SetEndpoint(const Value: string): IOAuth2Authenticator;
  • function SetGrantType(const Value: TGrantType): IOAuth2Authenticator;
  • function SetPassword(const Value: string): IOAuth2Authenticator;
  • function SetScope(const Value: string): IOAuth2Authenticator;
  • function SetUsername(const Value: string): IOAuth2Authenticator;

São propriedades expostas pela interface IOAuth2Authenticator:

  • AcrValues: string;
  • ClientID: string;
  • ClientSecret: string;
  • Endpoint: string;
  • GrantType: TGrantType;
  • Password: string;
  • Scope: string;
  • Username: string;
  • TokenExpiration: TDateTime ;
  • RefreshExpiration: TDateTime;

Para utilização da implementação da interface IOAuth2Authenticator em aplicações Delphi, segue o exemplo de uso deste componente.


Execução da ação de login:

Execução da ação de login
procedure TMainForm.btnSendClick(Sender: TObject);
  function GetGrantType: TGrantType;
  begin
    case cbxGrantType.ItemIndex of
      1: Result := UserPassword;
    else
      Result := ClientCredentials;
    end;
  end;

var
  Token: string;

const
  TOKEN_EXPIRATION   = 'Token Expiration: %s';
  REFRESH_EXPIRATION = 'Refresh Expiration: %s';
  EXCEPTION_MESSAGE  = 'Exception Type: %s'#13#10'Message: %s';
begin
  Screen.Cursor := crHourGlass;

  try
    try
      TOAuth2Authenticator.Instance.SetEndpoint(edtEndpoint.Text)
                                   .SetGrantType(GetGrantType)
                                   .SetClientID(edtClientID.Text)
                                   .SetClientSecret(edtClientSecret.Text)
                                   .SetUsername(edtUsername.Text)
                                   .SetPassword(edtPassword.Text)
                                   .GetAuthToken(Token);
      mmoToken.Lines.Text := Token;
      lblTokenExpiration.Caption   := TOKEN_EXPIRATION.Format([TOAuth2Authenticator.Instance.TokenExpiration.AsISO8601DateTime]);
      lblRefreshExpiration.Caption := REFRESH_EXPIRATION.Format([TOAuth2Authenticator.Instance.RefreshExpiration.AsISO8601DateTime]);
    except
      on E: Exception do
      begin
        Application.MessageBox(PWideChar(EXCEPTION_MESSAGE.Format([E.ClassName, E.Message])), 'Exceção', MB_OK or MB_ICONERROR);
      end;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

Resultado esperado:


04. DEMAIS INFORMAÇÕES

Por ser dependente do framework de desenvolvimento, os caminhos para os arquivos arquivos desta dependência devem estar presentes no projeto ao ser utilizado.

05. ASSUNTOS RELACIONADOS