0

I get a bearer Token from a azure-app registration in a angular web-app by

login(loginData: any): Observable<any> {
    const body = new HttpParams()
      .set('client_id',OAUTH_CLIENT)
      .set('client_secret',OAUTH_SECRET)
      .set('username', loginData.username)
      .set('password', loginData.password)
      .set('grant_type', 'client_credentials');

    return this.http.post<any>(API_URL, body, HTTP_OPTIONS)
      .pipe(
        tap(res => {
          this.tokenService.saveToken(res.access_token);
          window.open('./profile', '_self');
        }),
        catchError(AuthService.handleError)
      );

Result: "token_type": "Bearer", "expires_in": "3599", "ext_expires_in": "3599", "expires_on": "1678799845", "not_before": "1678795945", "resource": "00000002-0000-0000-c000-000000000000", "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyIsImtpZCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyJ9...

Now I need to validate this token or login with this token without given the credentials to the login.

When I use this:

 const body = new HttpParams()
      .set('grant_type', 'authorization_code')
      .set('client_id',OAUTH_CLIENT)
      .set('client_secret',OAUTH_SECRET)
      .set('code', token)
      .set('redirect_uri', this.redirectUrl);

    return this.http.post<any>(API_URL_Auth, body, HTTP_OPTIONS)
      .pipe(
        tap(res => {
          this.tokenService.saveToken(res.access_token);
          window.open(this.redirectUrl, '_self');
        }),
        catchError(AuthService.handleError)
      );

I get as result: invalid grant_type 'authorization_code'

{
    "error": "invalid_grant",
    "error_description": "AADSTS9002313: Invalid request. Request is malformed or invalid.\r\nTrace ID: 1da38d15-313a-4eed-af29-a094179da700\r\nCorrelation ID: f3b4998e-26e7-4820-9e0c-32bdb4449728\r\nTimestamp: 2023-03-14 13:44:44Z",
    "error_codes": [
        9002313
    ],
    "timestamp": "2023-03-14 13:44:44Z",
    "trace_id": "1da38d15-313a-4eed-af29-a094179da700",
    "correlation_id": "f3b4998e-26e7-4820-9e0c-32bdb4449728",
    "error_uri": "https://login.microsoftonline.com/error?code=9002313"
}

How can I do this ? Any settings on the app-registration needed to login with a token?

Micha
  • 906
  • 6
  • 9

0 Answers0