forked from Sunbird-Obsrv/obsrv-web-console
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
170 changed files
with
721 additions
and
2,419 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
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
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { PassportAuthProvider } from './passportAuthProvider'; | ||
import { KeycloakAuthProvider } from './keycloakAuthProvider'; | ||
import { BaseAuthProvider } from '../types'; | ||
|
||
export const authProviderFactory: (type: string, config?: any, sessionStore?: any) => BaseAuthProvider = (type, config, sessionStore) => { | ||
switch (type) { | ||
case 'keycloak': | ||
return new KeycloakAuthProvider(config, sessionStore); | ||
case 'basic': | ||
return new PassportAuthProvider(); | ||
default: | ||
throw new Error("Invalid authentication service type"); | ||
} | ||
}; | ||
export { BaseAuthProvider }; |
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,17 @@ | ||
import _ from 'lodash'; | ||
import { v4 } from 'uuid'; | ||
import bcrypt from 'bcryptjs'; | ||
import userService from '../services/oauthUsers'; | ||
|
||
export const userCreateAsBasic = async (userRequest: any) => { | ||
const { password } = userRequest; | ||
userRequest.password = await bcrypt.hash(password, 12); | ||
if (userRequest.mobile_number) { | ||
const { country_code, number } = userRequest.mobile_number; | ||
userRequest.mobile_number = `${String(country_code).trim()}_${String(number).trim()}`; | ||
} | ||
const userIdentifier = { id: v4(), created_on: new Date().toISOString() }; | ||
const userInfo = { ...userRequest, ...userIdentifier }; | ||
const result = await userService.save(userInfo); | ||
return result; | ||
}; |
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,106 @@ | ||
import axios from 'axios'; | ||
import appConfig from '../../shared/resources/appConfig'; | ||
import _ from 'lodash'; | ||
import userService from '../services/oauthUsers'; | ||
|
||
const keycloakUrl = appConfig.KEYCLOAK.URL; | ||
const keycloakHTTPClient = axios.create({ baseURL: keycloakUrl }); | ||
const keycloakRealm = appConfig.KEYCLOAK.REALM; | ||
|
||
export const authenticated = async (request: any) => { | ||
try { | ||
const userId = request?.kauth?.grant?.access_token?.content?.sub?.split(':'); | ||
const email_address = request?.kauth?.grant?.access_token?.content?.email; | ||
const preferred_username = request?.kauth?.grant?.access_token?.content?.preferred_username; | ||
|
||
request.session.userId = userId?.[userId.length - 1]; | ||
request.session.email_address = email_address; | ||
request.session.preferred_username = preferred_username; | ||
|
||
const user = await userService.find({ id: userId?.[0] }); | ||
request.session.roles = user?.roles; | ||
} catch (err) { | ||
console.log('user not authenticated', request?.kauth?.grant?.access_token?.content?.sub, err); | ||
} | ||
}; | ||
|
||
export const deauthenticated = function (request: any) { | ||
delete request?.session?.roles; | ||
delete request?.session?.userId; | ||
delete request?.session?.email_address; | ||
delete request?.session?.preferred_username; | ||
delete request?.session?.auth_redirect_uri; | ||
delete request?.session?.['keycloak-token']; | ||
|
||
if (request?.session) { | ||
request.session.sessionEvents = request?.session?.sessionEvents || []; | ||
delete request?.session?.sessionEvents; | ||
} | ||
}; | ||
|
||
export const userCreate = async (access_token: any, userRequest: any) => { | ||
const { user_name, email_address } = userRequest; | ||
const password = _.trim(userRequest.password); | ||
const payload = { | ||
email: email_address, | ||
username: user_name, | ||
enabled: true, | ||
credentials: [ | ||
{ | ||
type: 'password', | ||
value: password, | ||
temporary: false, | ||
}, | ||
], | ||
}; | ||
|
||
return keycloakHTTPClient | ||
.post(`/admin/realms/${keycloakRealm}/users`, payload, { | ||
headers: { | ||
Authorization: `Bearer ${access_token}`, | ||
}, | ||
}) | ||
.then((response) => { | ||
const location = _.get(response, 'headers.location'); | ||
const userId = location ? _.last(location.split('/')) : null; | ||
console.log('keyuser', userId); | ||
if (!userId) { | ||
throw new Error('UserId not found'); | ||
} | ||
return userId; | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
}; | ||
|
||
export const userCreateWithKeycloak = async (access_token: any, userRequest: any) => { | ||
const { user_name, email_address, roles } = userRequest; | ||
const id = await userCreate(access_token, userRequest); | ||
const created_on = new Date().toISOString(); | ||
const userInfo = { id, user_name, email_address, created_on, roles }; | ||
const result = await userService.save(userInfo); | ||
return result; | ||
}; | ||
|
||
export const keycloakLogout = async (req: any) => { | ||
const userId = req?.session?.userId; | ||
const access_token = req?.kauth?.grant?.access_token?.token; | ||
const refresh_token = req?.kauth?.grant?.refresh_token?.token; | ||
|
||
const data = new URLSearchParams({ | ||
client_id: req?.kauth?.grant?.access_token?.clientId, | ||
refresh_token: refresh_token, | ||
}); | ||
|
||
return keycloakHTTPClient | ||
.post(`admin/realms/${keycloakRealm}/users/${userId}/logout`, data, { | ||
headers: { | ||
Authorization: `Bearer ${access_token}`, | ||
}, | ||
}) | ||
.then() | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
}; |
Oops, something went wrong.