Openapi generator configuration for Azure B2C Authorization

Viewed 20

My Angular client application is using MSAL in order to communicate with Azure B2C and retrieve the id token, refresh token and access token. These are in my localstorage after login.

I'm also using openapi generator to generate my services for my client application. The thing is, whenever I make a call to my backend, it is resulting as a 401.

The controller:

        [Authorize]
        [RequiredScope("example.read")]
        [HttpGet(Name = "GetJobs")]
        public async Task<ActionResult<IEnumerable<JobDTO>>> Index(int pageNumber, int pageSize, string? titleDesc, string? category)
        {
            var owner = CheckClaimMatch(ClaimTypes.NameIdentifier);
            try
            {
                var result = await _jobModel.GetAllJobs(pageNumber, pageSize, titleDesc, category);
                return Ok(result);
            } catch (Exception ex)
            {
                return BadRequest(ex);
            }
        }

And here is my (simplified) openapi configuration:

openapi: 3.0.3
info:
  title: EXAMPLE REST API
  description: Api for the new example application
  version: 1.0.0
servers:
  - url: "https://{hostname}:{port}/{basePath}/"
    variables:
      hostname:
        default: localhost
      port:
        default: "7051"
      basePath:
        default: api
tags:
  - name: Job
paths:
  /Jobs:
    get:
      operationId: getJobs
      security:
        - AzureOAuth:
          - example.read
      tags:
        - Job
      parameters:
        - in: query
          name: titleDesc
          schema:
            type: string
      responses:
        200:
          description: Returns requested page
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/JobPage"
components:
  securitySchemes:
    AzureOAuth:
      type: oauth2
      description: This API uses OAuth2 with authorizationCode grant flow.
      flows:
        authorizationCode:
          authorizationUrl: https://example.b2clogin.com/example.onmicrosoft.com/B2C_1_SignUpAndSignIn/oauth2/v2.0/authorize
          tokenUrl: https://example.b2clogin.com/example.onmicrosoft.com/B2C_1_SignUpAndSignIn/oauth2/v2.0/token
          scopes:
            example.read: read
            example.write: write
  schemas:
    Job:
      type: object
      required:
        - title
        - category
        - teleworkingDays
      description: Job information
      properties:
        id:
          type: integer
          format: int64
        title:
          type: string   
security:
  - AzureOAuth:
    - example.read
    - example.write

I'm using oauth2 according to Azure's documentation.

Here is the post executed by the generated service.

post call from the service

Notice that the Bearer is empty.

I'm also wondering, if it's the correct way to do, because here I'm implementing it with the oauth2 way, but as I already have an access token in my localstorage, shouldn't just go with the bearer implementation ?

Also, testing with postman works completely fine, so I assume the controller part is fine.

Thank you

0 Answers
Related