This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
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.
breaking: remove SecurityService interface
- Loading branch information
Dara Hayes
committed
Jul 5, 2019
1 parent
ea4bcd8
commit 6b82d5e
Showing
17 changed files
with
106 additions
and
316 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,45 @@ | ||
import { Token } from './KeycloakToken' | ||
import { KeycloakSubscriptionHandlerOptions } from './api' | ||
|
||
export class KeycloakSubscriptionHandler { | ||
|
||
public readonly keycloakConfig: any | ||
public readonly schemaDirectives: any | ||
public readonly authContextProvider: any | ||
public keycloak: any | ||
|
||
constructor (options: KeycloakSubscriptionHandlerOptions) { | ||
if (!options.keycloak) { | ||
throw new Error('missing keycloak instance in options') | ||
} | ||
this.keycloak = options.keycloak | ||
} | ||
|
||
public async onSubscriptionConnect(connectionParams: any, webSocket: any, context: any): Promise<any> { | ||
if (!connectionParams || typeof connectionParams !== 'object') { | ||
throw new Error('Access Denied - missing connection parameters for Authentication') | ||
} | ||
const header = connectionParams.Authorization | ||
|| connectionParams.authorization | ||
|| connectionParams.Auth | ||
|| connectionParams.auth | ||
const clientId = connectionParams.clientId | ||
if (!header) { | ||
throw new Error('Access Denied - missing Authorization field in connection parameters') | ||
} | ||
const token = this.getBearerTokenFromHeader(header, clientId) | ||
try { | ||
await this.keycloak.grantManager.validateToken(token, 'Bearer') | ||
return token | ||
} catch (e) { | ||
throw new Error(`Access Denied - ${e}`) | ||
} | ||
} | ||
|
||
private getBearerTokenFromHeader(header: any, clientId?: string) { | ||
if (header && typeof header === 'string' && (header.indexOf('bearer ') === 0 || header.indexOf('Bearer ') === 0)) { | ||
const token = header.substring(7) | ||
return new Token(token, clientId) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
export interface KeycloakSubscriptionHandlerOptions { | ||
keycloak: any | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
export * from './SecurityService' | ||
export * from './DefaultSecurityService' | ||
export * from './AuthContextProvider' | ||
export * from './SecurityService' | ||
export * from './ApplyAuthMiddlewareOptions' | ||
export * from './KeycloakSecurityServiceOptions' | ||
export * from './KeycloakSubscriptionHandlerOptions' | ||
export * from './SchemaDirectives' |
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,24 @@ | ||
export const auth = (next: Function) => (root: any, args: any, context: any, info: any) => { | ||
if (!context.auth || !context.auth.isAuthenticated()) { | ||
throw new Error(`User not Authenticated`) | ||
} | ||
return next(root, args, context, info) | ||
} | ||
|
||
export const hasRole = (roles: Array<string>) => (next: Function) => (root: any, args: any, context: any, info: any) => { | ||
if (!context.auth || !context.auth.isAuthenticated()) { | ||
throw new Error(`User not Authenticated`) | ||
} | ||
|
||
let foundRole = null // this will be the role the user was successfully authorized on | ||
|
||
foundRole = roles.find((role: string) => { | ||
return context.auth.hasRole(role) | ||
}) | ||
|
||
if (!foundRole) { | ||
throw new Error(`User is not authorized. Must have one of the following roles: [${roles}]`) | ||
} | ||
|
||
return next(root, args, context, info) | ||
} |
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
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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './KeycloakSecurityService' | ||
export * from './KeycloakSubscriptionHandler' | ||
export * from './AuthContextProvider' | ||
export * from './directives' | ||
export * from './api' |
Oops, something went wrong.