Skip to content

Conversation

@itslenny
Copy link

Hi, we have a scenario where some of our endpoints are secured and some are not.

Using the interceptor pattern leaves us unable to determine if we should add the authorize header or not. As a solution I added an injectable angular token provider.

Basically... if there are "authorization" header values AND the auth type is set to bearer in the swagger file it won't include those as params that are needed to be provided by the API consumer, and will instead try to fetch the token using the api token provider as needed. Additionally, we have two different types of auth tokens authorization and device-authorization so this enables us to provide the correct token based on what the endpoint defines in the swagger file.

With this pattern we only append the auth header if it is required, and the individual places that we call the api service don't need to have or even know about the token.

This could be merged as-is, but I think it'd cause some breaking changes for anyone depending on sending auth tokens as a param. Perhaps i can be a cli flag?

We're using the forked version as-is, but I just wanted to push this change upstream in case you'd like to incorporate it or if anyone else may need similar functionality.

Usage...

add this to your app module

{ provide: ApiServiceTokenProviderInjectionToken, useClass: ApiTokenProviderServiceService },

create a provider service like this...

@Injectable()
export class ApiTokenProviderServiceService implements ApiServiceTokenProvider {

  getToken(name: string): Observable<string> {
    switch (name) {
      case 'authorization':
        // TODO: get token from wherever it is stored
        return of('');
      case 'device-authorization':
        // optional secondary auth types
        return of('');
      default:
        // unknown auth type
        return of(null);
    }
  }
}

@itslenny itslenny changed the title Add injectable token provider fro authenticated endpoints Add injectable token provider for authenticated endpoints Aug 17, 2020
@itslenny itslenny force-pushed the dev/lenny/add-token-provider branch from fe701dc to b5ed444 Compare August 17, 2020 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant