Skip to content

Commit aa428bd

Browse files
committed
avniproject/avni-server#397 | Support for Keycloak auth
1 parent bb26a1e commit aa428bd

20 files changed

+554
-268
lines changed

packages/openchs-android/package-lock.json

Lines changed: 18 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openchs-android/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@
4747
"i18n-js": "3.9.2",
4848
"immutable": "4.1.0",
4949
"invariant": "2.2.4",
50+
"jwt-decode": "^3.1.2",
5051
"lodash": "4.17.21",
5152
"moment": "2.29.4",
5253
"native-base": "3.4.9",
53-
"openchs-models": "1.27.28",
54+
"openchs-models": "1.27.29",
5455
"prop-types": "15.8.1",
5556
"react": "18.2.0",
5657
"react-native": "0.69.7",

packages/openchs-android/src/action/LoginActions.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import UserInfoService from "../service/UserInfoService";
55
import _ from 'lodash';
66
import {firebaseEvents, logEvent} from "../utility/Analytics";
77
import BackupRestoreRealmService from "../service/BackupRestoreRealm";
8+
import SettingsService from "../service/SettingsService";
89

910
class LoginActions {
1011
static getInitialState() {
@@ -19,15 +20,18 @@ class LoginActions {
1920
validationResult: ValidationResult.successful(),
2021
dumpRestoring: false,
2122
percentProgress: 0,
22-
dumpRestoreMessage: null
23+
dumpRestoreMessage: null,
24+
idpType: null
2325
};
2426
}
2527

2628
static onLoad(state, action, context) {
2729
const userInfo = context.get(UserInfoService).getUserInfo();
28-
const newState = _.assignIn({}, state, userInfo ? {userId: userInfo.username, loggedInUser: userInfo.username, percentProgress: null} : {
30+
const settings = context.get(SettingsService).getSettings();
31+
const newState = _.assignIn({}, state, userInfo ? {userId: userInfo.username, loggedInUser: userInfo.username, percentProgress: null, idpType: settings.idpType} : {
2932
percentProgress: null,
30-
dumpRestoreMessage: null
33+
dumpRestoreMessage: null,
34+
idpType: settings.idpType
3135
});
3236
newState.dumpRestoring = false;
3337
return newState;
@@ -48,7 +52,7 @@ class LoginActions {
4852

4953
static onLoginStarted(state, action, context) {
5054
let newState = _.assignIn({}, state, {loggingIn: true, loginError: '', loginSuccess: false});
51-
context.get(AuthService)
55+
context.get(AuthService).getAuthProviderService()
5256
.authenticate(state.userId, state.password)
5357
.then((response) => {
5458
if (response.status === "LOGIN_SUCCESS") {

packages/openchs-android/src/framework/http/requests.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ import General from "../../utility/General";
22
import _ from 'lodash';
33
import AuthenticationError from "../../service/AuthenticationError";
44
import ServerError from "../../service/ServerError";
5-
import Config from '../Config';
65
import GlobalContext from "../../GlobalContext";
76

87
const ACCEPTABLE_RESPONSE_STATUSES = [200, 201];
98

109
const getAuthToken = async () => {
1110
const authService = GlobalContext.getInstance().beanRegistry.getService("authService");
12-
return await authService.getAuthToken();
11+
return await authService.getAuthProviderService().getAuthToken();
1312
};
1413

14+
const getIdpType = async () => {
15+
const settingsService = GlobalContext.getInstance().beanRegistry.getService("settingsService");
16+
return await settingsService.getSettings().idpType;
17+
}
18+
1519
const fetchFactory = (endpoint, method = "GET", params, fetchWithoutTimeout) => {
1620
const processResponse = (response) => {
1721
if (ACCEPTABLE_RESPONSE_STATUSES.indexOf(parseInt(response.status)) > -1) {
@@ -52,8 +56,9 @@ const _addAuthIfRequired = async (request, bypassAuth) => {
5256
if (bypassAuth) {
5357
return request;
5458
}
55-
const token = await getAuthToken();
56-
return Config.ENV === 'dev' ?
59+
const [token, idpType] = await Promise.all([getAuthToken(), getIdpType()]);
60+
61+
return idpType === 'none' ?
5762
_.merge({}, request, {headers: {"USER-NAME": token}}) :
5863
_.merge({}, request, {headers: {'AUTH-TOKEN': token}});
5964
};
@@ -87,3 +92,8 @@ export let get = (endpoint, bypassAuth = false) => {
8792
export let getJSON = (endpoint, bypassAuth = false) => {
8893
return _get(endpoint, bypassAuth);
8994
};
95+
96+
export let postUrlFormEncoded = (endpoint, body) => {
97+
const formBody = new URLSearchParams(body).toString();
98+
return fetchFactory(endpoint, "POST", {headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*'}, body: formBody}, true)
99+
}

0 commit comments

Comments
 (0)