-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
|
||
} | ||
|