Skip to content

Commit

Permalink
add bearer auth class
Browse files Browse the repository at this point in the history
Add a new class inheriting form AuthorizationProvider which enables the use of OAuth2 style tokens for authentication.

Signed-off-by: Matthias Weirich <matthias.weirich@selectcode.de>
  • Loading branch information
vavido committed Dec 17, 2020
1 parent 55d5ca2 commit fd0575e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions javascript/lib/api/src/auth/bearer-auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

import { AuthProvider, DittoHeaders, DittoURL } from './auth-provider';

/**
* Provides OAuth-Style authentication via bearer tokens
*/
export abstract class BearerAuth implements AuthProvider {

/**
* The bearer token used for authentication
*/
protected token!: string;

/**
* Build a new instance of bearer auth
* @param token The bearer token to be used
*/
constructor(token: string) {
this.token = token;
}

abstract authenticateWithHeaders(originalHeaders: DittoHeaders): DittoHeaders;

abstract authenticateWithUrl(originalUrl: DittoURL): DittoURL;
}


/**
* Http implementation of bearer auth using the Authorization HTTP header.
*/
export class HttpBearerAuth extends BearerAuth {

/**
* Creates a new instance for HTTP connections
* @param token
*/
static newInstance(token: string): HttpBearerAuth {
return new HttpBearerAuth(token);
}

authenticateWithUrl(originalUrl: DittoURL): DittoURL {
return originalUrl;
}

authenticateWithHeaders(originalHeaders: DittoHeaders): DittoHeaders {
return originalHeaders.set('Authorization', `Bearer ${this.token}`);
}

}

0 comments on commit fd0575e

Please sign in to comment.