diff --git a/app/redux/actions/AuthActions.js b/app/redux/actions/AuthActions.js index 46aedaf7a..bc4e4a133 100644 --- a/app/redux/actions/AuthActions.js +++ b/app/redux/actions/AuthActions.js @@ -13,8 +13,9 @@ import { GET_USER_LOCATION, } from './types' -export const getOAuth2Config = () => ({ +export const getOAuth2Config = (token) => ({ type: GET_OAUTH2_CONFIG, + payload: { token }, }) export const getOAuth2ConfigResponse = (config) => ({ diff --git a/app/redux/actions/LicenseActions.js b/app/redux/actions/LicenseActions.js index 152426130..80b604b71 100644 --- a/app/redux/actions/LicenseActions.js +++ b/app/redux/actions/LicenseActions.js @@ -5,8 +5,9 @@ import { ACTIVATE_LICENSE_RESPONSE, } from './types' - export const checkLicensePresent = () => ({ + export const checkLicensePresent = (token) => ({ type: CHECK_FOR_VALID_LICENSE, + payload: { token }, }) export const checkLicensePresentResponse = (isLicenseValid) => ({ @@ -14,9 +15,9 @@ import { payload: { isLicenseValid }, }) - export const activateLicense = (licenseKey) => ({ + export const activateLicense = (licenseKey, token) => ({ type: ACTIVATE_LICENSE, - payload: { licenseKey }, + payload: { licenseKey, token }, }) export const activateLicenseResponse = (isLicenseValid) => ({ diff --git a/app/redux/api/backend-api.js b/app/redux/api/backend-api.js index 338e822f0..b29d84b5b 100644 --- a/app/redux/api/backend-api.js +++ b/app/redux/api/backend-api.js @@ -2,9 +2,10 @@ import axios from '../api/axios' import axios_instance from 'axios' // Get OAuth2 Configuration -export const fetchServerConfiguration = async () => { +export const fetchServerConfiguration = async (token) => { + const headers = { Authorization: `Bearer ${token}`}; return axios - .get('/oauth2/config') + .get('/oauth2/config', {headers}) .then((response) => response.data) .catch((error) => { console.error( @@ -71,10 +72,24 @@ export const fetchApiAccessToken = async (jwt) => { }) } +export const fetchApiTokenWithDefaultScopes = async () => { + return axios + .get('/oauth2/api-protection-token') + .then((response) => response.data) + .catch((error) => { + console.error( + 'Problems getting API access token in order to process api calls.', + error, + ) + return -1 + }) +} + // Check License present -export const checkLicensePresent = async () => { +export const checkLicensePresent = async (token) => { + const headers = { Authorization: `Bearer ${token}`}; return axios - .get('/license/checkLicense') + .get('/license/checkLicense', {headers}) .then((response) => response.data) .catch((error) => { console.error('Error checking license of admin-ui', error) @@ -83,12 +98,13 @@ export const checkLicensePresent = async () => { } // Activate license using key -export const activateLicense = async (licenseKey) => { +export const activateLicense = async (licenseKey, token) => { let data = { licenseKey: licenseKey } return axios .post('/license/activateLicense', data, { headers: { 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` }, }) .then((response) => response.data) diff --git a/app/redux/sagas/AuthSaga.js b/app/redux/sagas/AuthSaga.js index 483a0def0..1edcbef06 100644 --- a/app/redux/sagas/AuthSaga.js +++ b/app/redux/sagas/AuthSaga.js @@ -20,11 +20,18 @@ import { fetchUserInformation, fetchApiAccessToken, getUserIpAndLocation, + fetchApiTokenWithDefaultScopes, } from '../api/backend-api' +function* getApiTokenWithDefaultScopes() { + const response = yield call(fetchApiTokenWithDefaultScopes) + return response.access_token +} + function* getOAuth2ConfigWorker() { try { - const response = yield call(fetchServerConfiguration) + const token = yield* getApiTokenWithDefaultScopes(); + const response = yield call(fetchServerConfiguration, token) if (response) { yield put(getOAuth2ConfigResponse(response)) return @@ -54,6 +61,12 @@ function* getAPIAccessTokenWorker({ payload }) { yield put(getAPIAccessTokenResponse(response)) return } + } else { + const token = yield* getApiTokenWithDefaultScopes() + if (token) { + yield put(getAPIAccessTokenWithDefaultScopesResponse(token)) + return + } } } catch (error) { console.log('Problems getting API Access Token.', error) diff --git a/app/redux/sagas/LicenseSaga.js b/app/redux/sagas/LicenseSaga.js index 21ddb9eba..db3e8e798 100644 --- a/app/redux/sagas/LicenseSaga.js +++ b/app/redux/sagas/LicenseSaga.js @@ -8,11 +8,17 @@ import { activateLicenseResponse, } from '../actions' -import { checkLicensePresent, activateLicense } from '../api/backend-api' +import { checkLicensePresent, activateLicense, fetchApiTokenWithDefaultScopes } from '../api/backend-api' + +function* getApiTokenWithDefaultScopes() { + const response = yield call(fetchApiTokenWithDefaultScopes) + return response.access_token +} function* checkLicensePresentWorker() { try { - const response = yield call(checkLicensePresent) + const token = yield* getApiTokenWithDefaultScopes(); + const response = yield call(checkLicensePresent, token) if (response) { yield put(checkLicensePresentResponse(response)) return @@ -25,7 +31,8 @@ function* checkLicensePresentWorker() { function* activateLicenseWorker({ payload }) { try { - const response = yield call(activateLicense, payload.licenseKey) + const token = yield* getApiTokenWithDefaultScopes(); + const response = yield call(activateLicense, payload.licenseKey, token) if (response) { yield put(activateLicenseResponse(response)) return diff --git a/app/utils/PermChecker.js b/app/utils/PermChecker.js index 65e708d38..3173a02d1 100644 --- a/app/utils/PermChecker.js +++ b/app/utils/PermChecker.js @@ -16,6 +16,9 @@ export const ROLE_READ = 'https://jans.io/adminui/user/role.read' export const ROLE_WRITE = 'https://jans.io/adminui/user/role.write' export const ROLE_DELETE = 'https://jans.io/adminui/user/role.delete' +export const LICENSE_DETAILS_READ = BASE_URL + '/config/adminui/license.readonly' +export const LICENSE_DETAILS_WRITE = BASE_URL + '/config/adminui/license.write' + export const PERMISSION_READ = 'https://jans.io/adminui/user/permission.read' export const PERMISSION_WRITE = 'https://jans.io/adminui/user/permission.write' export const PERMISSION_DELETE = diff --git a/plugins/admin/common/Constants.js b/plugins/admin/common/Constants.js index 50b1d6a5b..9d0cf67f8 100644 --- a/plugins/admin/common/Constants.js +++ b/plugins/admin/common/Constants.js @@ -12,5 +12,6 @@ export const FETCHING_SCOPES = 'Fetch scopes' export const SEARCHING_SCRIPTS = 'Search scripts' export const FETCHING_SCRIPTS = 'Fetch scripts' +export const FETCHING_LICENSE_DETAILS = 'Fetch license details' diff --git a/plugins/admin/components/Configuration/LicenseDetailsPage.js b/plugins/admin/components/Configuration/LicenseDetailsPage.js index f0f4f38ab..da5fba8cc 100644 --- a/plugins/admin/components/Configuration/LicenseDetailsPage.js +++ b/plugins/admin/components/Configuration/LicenseDetailsPage.js @@ -17,17 +17,29 @@ import { Row, Col, } from '../../../../app/components' +import { + buildPayload, +} from '../../../../app/utils/PermChecker' import GluuLoader from '../../../../app/routes/Apps/Gluu/GluuLoader' import Alert from '@material-ui/lab/Alert' +import { + FETCHING_LICENSE_DETAILS, +} from '../../common/Constants' function LicenseDetailsPage({ item, loading, dispatch }) { + const userAction = {} + const options = {} + useEffect(() => { - dispatch(getLicenseDetails()) + buildPayload(userAction, FETCHING_LICENSE_DETAILS, options) + dispatch(getLicenseDetails({})) }, []) + function handleSubmit(data) { if (data) { - dispatch(updateLicenseDetails(data)) + buildPayload(userAction, 'message', data) + dispatch(updateLicenseDetails(userAction)) } } diff --git a/plugins/admin/redux/actions/LicenseDetailsActions.js b/plugins/admin/redux/actions/LicenseDetailsActions.js index e32519133..487377212 100644 --- a/plugins/admin/redux/actions/LicenseDetailsActions.js +++ b/plugins/admin/redux/actions/LicenseDetailsActions.js @@ -5,8 +5,9 @@ import { UPDATE_LICENSE_DETAILS_RESPONSE, } from './types' -export const getLicenseDetails = () => ({ +export const getLicenseDetails = (action) => ({ type: GET_LICENSE_DETAILS, + payload: { action }, }) export const getLicenseDetailsResponse = (data) => ({ @@ -14,9 +15,9 @@ export const getLicenseDetailsResponse = (data) => ({ payload: { data }, }) -export const updateLicenseDetails = (data) => ({ +export const updateLicenseDetails = (action) => ({ type: UPDATE_LICENSE_DETAILS, - payload: { data }, + payload: { action }, }) export const updateLicenseDetailsResponse = (data) => ({ diff --git a/plugins/admin/redux/api/LicenseDetailsApi.js b/plugins/admin/redux/api/LicenseDetailsApi.js index db989d349..4bb93ecf0 100644 --- a/plugins/admin/redux/api/LicenseDetailsApi.js +++ b/plugins/admin/redux/api/LicenseDetailsApi.js @@ -1,26 +1,32 @@ -import axios from '../../../../app/redux/api/axios' +export default class LicenseDetailsApi { + constructor(api) { + this.api = api + } -// Get License Details -export const getLicenseDetails = async () => { - return axios - .get('/license/getLicenseDetails') - .then((response) => response.data) - .catch((error) => { - console.error('Problems getting license details.', error) - return -1 + getLicenseDetails = () => { + return new Promise((resolve, reject) => { + this.api.getAdminuiLicense((error, data) => { + this.handleResponse(error, reject, resolve, data) + }) }) -} -//update License Details -export const updateLicenseDetails = async (licenseDetails) => { - return axios - .put('/license/updateLicenseDetails', licenseDetails, { - headers: { - 'Content-Type': 'application/json', - }, - }) - .then((response) => response) - .catch((error) => { - console.error('Problems updating license details.', error) - return -1 + } + + updateLicenseDetails = (data) => { + const options = {} + options['licenseRequest'] = data + return new Promise((resolve, reject) => { + this.api.editAdminuiLicense(options, (error, options) => { + alert(JSON.stringify(data)) + this.handleResponse(error, reject, resolve, data) + }) }) -} + } + + handleResponse(error, reject, resolve, data) { + if (error) { + reject(error) + } else { + resolve(data) + } + } +} \ No newline at end of file diff --git a/plugins/admin/redux/sagas/LicenseDetailsSaga.js b/plugins/admin/redux/sagas/LicenseDetailsSaga.js index b921f0df5..6f4f5a573 100644 --- a/plugins/admin/redux/sagas/LicenseDetailsSaga.js +++ b/plugins/admin/redux/sagas/LicenseDetailsSaga.js @@ -1,42 +1,66 @@ -import { all, call, fork, put, takeEvery } from 'redux-saga/effects' +import { all, call, fork, put, takeEvery, select } from 'redux-saga/effects' import { GET_LICENSE_DETAILS, UPDATE_LICENSE_DETAILS } from '../actions/types' import { getLicenseDetailsResponse, updateLicenseDetailsResponse, } from '../actions/LicenseDetailsActions' - +import { getClient } from '../../../../app/redux/api/base' +import LicenseDetailsApi from '../api/LicenseDetailsApi' +const JansConfigApi = require('jans_config_api') +import { initAudit } from '../../../../app/redux/sagas/SagaUtils' import { - getLicenseDetails, - updateLicenseDetails, -} from '../api/LicenseDetailsApi' + isFourZeroOneError, + addAdditionalData, +} from '../../../../app/utils/TokenController' +import { getAPIAccessToken } from '../../../../app/redux/actions/AuthActions' + +function* newFunction() { + const token = yield select((state) => state.authReducer.token.access_token) + const issuer = yield select((state) => state.authReducer.issuer) + const api = new JansConfigApi.AdminUILicenseApi( + getClient(JansConfigApi, token, issuer), + ) + return new LicenseDetailsApi(api) +} + -function* getLicenseDetailsWorker() { +export function* getLicenseDetailsWorker({ payload }) { + const audit = yield* initAudit() try { - const response = yield call(getLicenseDetails) - if (response) { - yield put(getLicenseDetailsResponse(response)) - return + //addAdditionalData(audit, FETCH, GET_LICENSE_DETAILS, payload) + const licenseApi = yield* newFunction() + const data = yield call(licenseApi.getLicenseDetails) + yield put(getLicenseDetailsResponse(data)) + yield call(postUserAction, audit) + } catch (e) { + yield put(getLicenseDetailsResponse(null)) + if (isFourZeroOneError(e)) { + const jwt = yield select((state) => state.authReducer.userinfo_jwt) + yield put(getAPIAccessToken(jwt)) } - } catch (error) { - console.log('Error in fetching License Details.', error) } - yield put(getLicenseDetailsResponse()) } -function* updateLicenseDetailsWorker({ payload }) { +export function* updateLicenseDetailsWorker({ payload }) { + const audit = yield* initAudit() try { - const response = yield call(updateLicenseDetails, payload.data) - if (response) { - yield put(updateLicenseDetailsResponse(response)) - return + //addAdditionalData(audit, UPDATE, UPDATE_LICENSE_DETAILS, payload) + const roleApi = yield* newFunction() + const data = yield call(roleApi.updateLicenseDetails, payload.action.action_data) + yield put(updateLicenseDetailsResponse(data)) + yield call(postUserAction, audit) + } catch (e) { + yield put(updateLicenseDetailsResponse(null)) + if (isFourZeroOneError(e)) { + const jwt = yield select((state) => state.authReducer.userinfo_jwt) + yield put(getAPIAccessToken(jwt)) } - } catch (error) { - console.log('Error in fetching License Details.', error) } - yield put(updateLicenseDetailsResponse()) } + export function* getLicenseWatcher() { + yield takeEvery(GET_LICENSE_DETAILS, getLicenseDetailsWorker) } @@ -46,4 +70,4 @@ export function* updateLicenseWatcher() { export default function* rootSaga() { yield all([fork(getLicenseWatcher), fork(updateLicenseWatcher)]) -} +} \ No newline at end of file