Skip to content

Commit

Permalink
feat(authentication-service): added keycloak signup provider (#89)
Browse files Browse the repository at this point in the history
* feat(authentication-service): added keycloak signup provider

added keycloak signup provider

* style(authentication-service): sonar fix

sonar fix
  • Loading branch information
IshuSF authored Oct 30, 2020
1 parent 905a0cf commit 8f37477
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
6 changes: 5 additions & 1 deletion services/authentication-service/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {repositories} from './repositories';
import {MySequence} from './sequence';
import {IAuthServiceConfig} from './types';
import {KeycloakVerifyProvider} from './modules/auth/providers/keycloak-verify.provider';
import {GoogleOauth2SignupProvider, SignUpBindings} from './providers';
import {GoogleOauth2SignupProvider, SignUpBindings, KeyCloakSignUpBindings} from './providers';
import { KeyCloakSignupProvider } from './providers/keycloak-signup.provider';

export class AuthenticationServiceComponent implements Component {
constructor(
Expand Down Expand Up @@ -144,6 +145,9 @@ export class AuthenticationServiceComponent implements Component {
this.providers[
Strategies.Passport.KEYCLOAK_VERIFIER.key
] = KeycloakVerifyProvider;
this.providers[
KeyCloakSignUpBindings.KEYCLOAK_SIGN_UP_PROVIDER.key
] = KeyCloakSignupProvider;
this.providers[
SignUpBindings.GOOGLE_SIGN_UP_PROVIDER.key
] = GoogleOauth2SignupProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {Provider} from '@loopback/context';
import {Provider, inject} from '@loopback/context';
import {repository} from '@loopback/repository';
import {HttpErrors} from '@loopback/rest';
import {AuthErrorKeys, VerifyFunction} from 'loopback4-authentication';

import {UserCredentialsRepository, UserRepository} from '../../../repositories';
import {AuthUser} from '../models/auth-user.model';
import { KeyCloakSignUpBindings } from '../../../providers/keys';
import { KeyCloakSignUpFn } from '../../../providers/types';

export class KeycloakVerifyProvider
implements Provider<VerifyFunction.KeycloakAuthFn> {
Expand All @@ -13,17 +15,24 @@ export class KeycloakVerifyProvider
public userRepository: UserRepository,
@repository(UserCredentialsRepository)
public userCredsRepository: UserCredentialsRepository,
@inject(KeyCloakSignUpBindings.KEYCLOAK_SIGN_UP_PROVIDER)
private readonly signupProvider: KeyCloakSignUpFn,
) {}

value(): VerifyFunction.KeycloakAuthFn {
return async (accessToken, refreshToken, profile) => {
const user = await this.userRepository.findOne({
let user = await this.userRepository.findOne({
where: {
email: profile.email,
},
});
if (!user) {
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials);
const newUser = await this.signupProvider(profile);
if (newUser) {
user = newUser;
} else {
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials);
}
}
const creds = await this.userCredsRepository.findOne({
where: {
Expand Down
1 change: 1 addition & 0 deletions services/authentication-service/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './keys';
export * from './types';
export * from './google-oauth2-signup.provider';
export * from './keycloak-signup.provider';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Provider} from '@loopback/context';
import {HttpErrors} from '@loopback/rest';
import {KeyCloakSignUpFn} from './types';
import {AuthErrorKeys} from 'loopback4-authentication/index';

export class KeyCloakSignupProvider implements Provider<KeyCloakSignUpFn> {
constructor() {}

value(): KeyCloakSignUpFn {
return async profile => {
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials);
};
}
}
8 changes: 7 additions & 1 deletion services/authentication-service/src/providers/keys.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {BindingKey} from '@loopback/core';
import {GoogleSignUpFn} from './types';
import {GoogleSignUpFn, KeyCloakSignUpFn} from './types';

export namespace SignUpBindings {
export const GOOGLE_SIGN_UP_PROVIDER = BindingKey.create<GoogleSignUpFn>(
'sf.google.signup.provider',
);
}

export namespace KeyCloakSignUpBindings {
export const KEYCLOAK_SIGN_UP_PROVIDER = BindingKey.create<KeyCloakSignUpFn>(
'sf.keycloak.signup.provider',
);
}
5 changes: 5 additions & 0 deletions services/authentication-service/src/providers/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as GoogleStrategy from 'passport-google-oauth20';
import {User, UserRelations} from '../models';
import { KeycloakProfile } from 'loopback4-authentication';

export interface GoogleSignUpFn {
(profile: GoogleStrategy.Profile): Promise<(User & UserRelations) | null>;
}

export interface KeyCloakSignUpFn {
(profile: KeycloakProfile): Promise<(User & UserRelations) | null>;
}

0 comments on commit 8f37477

Please sign in to comment.