@avanio/auth-header
v0.0.4
Published
Typescript/Javascript http auth header utils
Downloads
58
Readme
@avanio/auth-header
Typescript/Javascript http auth header class and parser.
Package includes:
- Type: AuthHeaderLikeString - type and type guard for string that this might look like http auth header
- Type: AuthHeaderString - type and type guard for string that is http auth header
- Type: AuthHeaderObject - type and type guard for object that have auth type and credentials
- Type: AuthHeaderType - type and type guard for string that is http auth header type
- Type: AuthHeaderCredentials - type and guard for string that is http auth header credentials
- Class: AuthHeader - class that represents http auth header and implements AuthHeaderObject
- Class: AuthHeaderError - class that represents http auth header errors
- Function: getAuthString - function that returns normalized auth header string
- Function: getAuthType - function that returns normalized auth header type
- Function: getAuthCredentials - function that returns auth header credentials
- Function: getAuthObject - function that returns AuthHeaderObject
examples
Handle both raw jwt token string and http bearer jwt token string
import {AuthHeader, isAuthHeaderLikeString} from '@avanio/auth-header';
function handleBearerToken(rawTokenOrAuthBearer: string): void {
const currentToken: string | AuthHeader = isAuthHeaderLikeString(rawTokenOrAuthBearer) ? AuthHeader.fromString(rawTokenOrAuthBearer) : rawTokenOrAuthBearer;
// if this is AuthHeader we are only interested Bearer tokens
if (currentToken instanceof AuthHeader && currentToken.type !== 'BEARER') {
throw new Error('token header: wrong authentication header type');
}
const token: string = currentToken instanceof AuthHeader ? currentToken.credentials : currentToken;
// do validate token
}