1

I am using angular-oauth2-oidc (https://www.npmjs.com/package/angular-oauth2-oidc) for getting a token for OAuth authorization. I am also using swagger for generating client API-files, which i must say is pretty sweet. I am using the old version of the online editor for generating my client side files. My very shortened swagger yaml:

  /fetchData:
    x-swagger-router-controller: controllerName
    post:
      operationId: oId
      tags:
        - tag
      security:
        - OauthSecurity: []
      parameters:
        - name: searchParameters
          in: body
          required: true
          schema:
            $ref: "#/definitions/definitionName"
      responses:
        200:
          description: Success
          schema:
            $ref: "#/definitions/responseName"

  securityDefinitions:
    OauthSecurity:
      type: oauth2
      flow: password
      tokenUrl: 'http://url/token'

In order to add the Authorization header in my requests I need to provide a configuration to the generated files. Copy-paste from one of the generated files:

if (this.configuration.accessToken) {
    let accessToken = typeof this.configuration.accessToken === 'function'
        ? this.configuration.accessToken()
        : this.configuration.accessToken;
    headers.set('Authorization', 'Bearer ' + accessToken);
}

My issue is that I am storing my access_token in SessionStorage and i want the generated files to use the value from that storage instead of having to provide it as the solution suggests at Configuring OAuth2 access token to typescript-angular2 client.

Is there any way of achieving this without having to edit the generated files from the swagger editor? Is there any easier ways of providing configuration to -all- of my different generated files?

Thank you for your answer(s)!

Jari Thorup Palo
  • 519
  • 1
  • 6
  • 17
  • the only workaround i find is to add "this.generatedApiName.configuration.accessToken = sessionStorage.access_token;" before doing my API calls. This works for now i guess but in the long run i think it only creates opportunities for failure later on. – Jari Thorup Palo Jun 20 '17 at 15:08

0 Answers0