Skip to content

Commit

Permalink
feat(licence): implement license key
Browse files Browse the repository at this point in the history
  • Loading branch information
mjatin-dev committed Apr 28, 2022
1 parent 57807c1 commit eab2249
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 79 deletions.
67 changes: 44 additions & 23 deletions admin-ui/app/redux/actions/LicenseActions.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
import {
CHECK_FOR_VALID_LICENSE,
CHECK_FOR_VALID_LICENSE_RESPONSE,
ACTIVATE_LICENSE,
ACTIVATE_LICENSE_RESPONSE,
} from './types'

export const checkLicensePresent = (token) => ({
type: CHECK_FOR_VALID_LICENSE,
payload: { token },
})

export const checkLicensePresentResponse = (isLicenseValid) => ({
type: CHECK_FOR_VALID_LICENSE_RESPONSE,
payload: { isLicenseValid },
})
CHECK_FOR_VALID_LICENSE,
CHECK_FOR_VALID_LICENSE_RESPONSE,
ACTIVATE_LICENSE,
ACTIVATE_LICENSE_RESPONSE,
ACTIVATE_CHECK_USER_API,
ACTIVATE_CHECK_LICENCE_API_VALID,
ACTIVATE_CHECK_USER_LICENSE_KEY_RESPONSE,
ACTIVATE_CHECK_USER_LICENSE_KEY,
} from './types'

export const activateLicense = (licenseKey, token) => ({
type: ACTIVATE_LICENSE,
payload: { licenseKey, token },
})
export const checkLicensePresent = (token) => ({
type: CHECK_FOR_VALID_LICENSE,
payload: { token },
})

export const activateLicenseResponse = (isLicenseValid) => ({
type: ACTIVATE_LICENSE_RESPONSE,
payload: { isLicenseValid },
})
export const checkUserApi = (payload) => ({
type: ACTIVATE_CHECK_USER_API,
payload: { payload },
})

export const checkUserLicenceKey = (payload) => ({
type: ACTIVATE_CHECK_USER_LICENSE_KEY,
payload: { payload },
})
export const checkUserApiKeyResponse = (payload) => ({
type: ACTIVATE_CHECK_LICENCE_API_VALID,
payload: payload,
})
export const checkUserLicenseKeyResponse = (payload) => ({
type: ACTIVATE_CHECK_USER_LICENSE_KEY_RESPONSE,
payload: payload,
})
export const checkLicensePresentResponse = (isLicenseValid) => ({
type: CHECK_FOR_VALID_LICENSE_RESPONSE,
payload: { isLicenseValid },
})

export const activateLicense = (licenseKey, token) => ({
type: ACTIVATE_LICENSE,
payload: { licenseKey, token },
})

export const activateLicenseResponse = (isLicenseValid) => ({
type: ACTIVATE_LICENSE_RESPONSE,
payload: { isLicenseValid },
})
8 changes: 7 additions & 1 deletion admin-ui/app/redux/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export const CHECK_FOR_VALID_LICENSE_RESPONSE =
'CHECK_FOR_VALID_LICENSE_RESPONSE'
export const ACTIVATE_LICENSE = 'ACTIVATE_LICENSE'
export const ACTIVATE_LICENSE_RESPONSE = 'ACTIVATE_LICENSE_RESPONSE'
export const ACTIVATE_CHECK_USER_API = 'ACTIVATE_CHECK_USER_API'
export const ACTIVATE_CHECK_USER_LICENSE_KEY = 'ACTIVATE_CHECK_USER_LICENSE_KEY'
export const ACTIVATE_CHECK_USER_LICENSE_KEY_RESPONSE =
'ACTIVATE_CHECK_USER_LICENSE_KEY_RESPONSE'
export const ACTIVATE_CHECK_LICENCE_API_VALID =
'ACTIVATE_CHECK_LICENCE_API_VALID'
//OIDC DISCOVERY
export const GET_OIDC_DISCOVERY = 'GET_OIDC_DISCOVERY'
export const GET_OIDC_DISCOVERY_RESPONSE = 'GET_OIDC_DISCOVERY_RESPONSE'
Expand All @@ -76,4 +82,4 @@ export const GET_HEALTH_RESPONSE = 'GET_HEALTH_RESPONSE'
export const GET_LICENSE_DETAILS = 'GET_LICENSE_DETAILS'
export const GET_LICENSE_DETAILS_RESPONSE = 'GET_LICENSE_DETAILS_RESPONSE'
export const UPDATE_LICENSE_DETAILS = 'UPDATE_LICENSE_DETAILS'
export const UPDATE_LICENSE_DETAILS_RESPONSE = 'UPDATE_LICENSE_DETAILS_RESPONSE'
export const UPDATE_LICENSE_DETAILS_RESPONSE = 'UPDATE_LICENSE_DETAILS_RESPONSE'
69 changes: 69 additions & 0 deletions admin-ui/app/redux/api/LicenseApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export default class LicenseApi {
constructor(api) {
this.api = api
}
getIsActive = () => {
return new Promise((resolve, reject) => {
this.api.isLicenseActive((error, data) => {
this.handleResponse(error, reject, resolve, data)
})
})
}

submitApiKey = (data) => {
const options = {}
options['licenseSpringCredentials'] = data
return new Promise((resolve, reject) => {
this.api.saveLicenseApiCredentials(options, (error, data) => {
this.handleResponse(error, reject, resolve, data)
})
})
}
submitLicenseKey = (data) => {
const options = {}
options['licenseApiRequest'] = data
return new Promise((resolve, reject) => {
this.api.activateAdminuiLicense(options, (error, data) => {
this.handleResponse(error, reject, resolve, data)
})
})
}

addPermission = (data) => {
const options = {}
options['adminPermission'] = data
return new Promise((resolve, reject) => {
this.api.addAdminuiPermission(options, (error, data) => {
this.handleResponse(error, reject, resolve, data)
})
})
}

editPermission = (data) => {
const options = {}
options['adminPermission'] = data
return new Promise((resolve, reject) => {
this.api.editAdminuiPermission(options, (error, data) => {
this.handleResponse(error, reject, resolve, data)
})
})
}
permission
deletePermission = async (data) => {
const options = {}
options['adminPermission'] = data
return new Promise((resolve, reject) => {
this.api.deleteAdminuiPermission(options, (error, data) => {
this.handleResponse(error, reject, resolve, data)
})
})
}

handleResponse(error, reject, resolve, data) {
if (error) {
reject(error)
} else {
resolve(data)
}
}
}
18 changes: 18 additions & 0 deletions admin-ui/app/redux/api/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ export const getClient = (JansConfigApi, r_token, r_issuer) => {
jansauth.accessToken = r_token
return defaultClient
}
export const getClientWithToken = (JansConfigApi, token) => {
const defaultClient = JansConfigApi.ApiClient.instance
defaultClient.timeout = 50000
const jansauth = defaultClient.authentications['oauth2']
defaultClient.basePath =
process.env.CONFIG_API_BASE_URL ||
'https://admin-ui-test.gluu.org'.replace(/\/+$/, '')
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers':
'Origin, X-Requested-With, Content-Type, Accept',
'Content-Type': 'application/json',
'Access-Control-Allow-Credentials': true,
Authorization: 'Bearer ' + token,
}
defaultClient.defaultHeaders = headers
return defaultClient
}
55 changes: 52 additions & 3 deletions admin-ui/app/redux/reducers/LicenseReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ import {
CHECK_FOR_VALID_LICENSE_RESPONSE,
ACTIVATE_LICENSE,
ACTIVATE_LICENSE_RESPONSE,
ACTIVATE_CHECK_USER_API,
ACTIVATE_CHECK_LICENCE_API_VALID,
ACTIVATE_CHECK_USER_LICENSE_KEY_RESPONSE,
ACTIVATE_CHECK_USER_LICENSE_KEY,
} from '../actions/types'
import reducerRegistry from './ReducerRegistry';
import reducerRegistry from './ReducerRegistry'

const INIT_STATE = {
isLicenseValid: false,
islicenseCheckResultLoaded: false,
isLicenseActivationResultLoaded: false,
isLicenceAPIkeyValid: false,
isLoading: false,
error: '',
}

const reducerName = 'licenseReducer';
const reducerName = 'licenseReducer'

export default function licenseReducer(state = INIT_STATE, action) {
switch (action.type) {
Expand All @@ -21,6 +28,48 @@ export default function licenseReducer(state = INIT_STATE, action) {
...state,
islicenseCheckResultLoaded: false,
}
case ACTIVATE_CHECK_USER_API:
return {
...state,
isLoading: true,
error: '',
}
case ACTIVATE_CHECK_USER_LICENSE_KEY:
return {
...state,
isLoading: true,
error: '',
}
case ACTIVATE_CHECK_LICENCE_API_VALID:
if (action.payload.apiResult) {
return {
...state,
isLicenceAPIkeyValid: action.payload,
error: '',
isLoading: false,
}
} else {
return {
...state,
error: action.payload.responseMessage,
isLoading: false,
}
}
case ACTIVATE_CHECK_USER_LICENSE_KEY_RESPONSE:
if (action.payload.apiResult) {
return {
...state,
isLicenseValid: action.payload.apiResult,
error: '',
isLoading: false,
}
} else {
return {
...state,
error: action.payload.responseMessage,
isLoading: false,
}
}
case CHECK_FOR_VALID_LICENSE_RESPONSE:
if (action.payload.isLicenseValid) {
return {
Expand Down Expand Up @@ -59,4 +108,4 @@ export default function licenseReducer(state = INIT_STATE, action) {
}
}
}
reducerRegistry.register(reducerName, licenseReducer);
reducerRegistry.register(reducerName, licenseReducer)
66 changes: 55 additions & 11 deletions admin-ui/app/redux/sagas/LicenseSaga.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
/**
* License Sagas
*/
import { all, call, fork, put, takeEvery } from 'redux-saga/effects'
import { CHECK_FOR_VALID_LICENSE, ACTIVATE_LICENSE } from '../actions/types'
import { all, call, fork, put, take, takeEvery } from 'redux-saga/effects'
import {
CHECK_FOR_VALID_LICENSE,
ACTIVATE_LICENSE,
ACTIVATE_CHECK_USER_API,
ACTIVATE_CHECK_USER_LICENSE_KEY,
} from '../actions/types'
import {
checkLicensePresentResponse,
activateLicenseResponse,
} from '../actions'

import LicenseApi from '../api/LicenseApi'
import { getClient, getClientWithToken } from '../api/base'
import {
checkUserApiKeyResponse,
checkUserLicenseKeyResponse,
} from '../actions'
import {
checkLicensePresent,
activateLicense,
fetchApiTokenWithDefaultScopes,
} from '../api/backend-api'

const JansConfigApi = require('jans_config_api')

function* getApiTokenWithDefaultScopes() {
const response = yield call(fetchApiTokenWithDefaultScopes)
return response.access_token
const api = new JansConfigApi.AdminUILicenseApi(
getClientWithToken(JansConfigApi, response.access_token),
)
return new LicenseApi(api)
// return response.access_token
}

function* checkLicensePresentWorker() {
try {
// const issuer = yield select((state) => state.authReducer.issuer)
const token = yield* getApiTokenWithDefaultScopes()
const response = yield call(checkLicensePresent, token)
// if (response) {
// yield put(checkLicensePresentResponse(response))
// return
// }
const licenseApi = yield* getApiTokenWithDefaultScopes()
const response = yield call(licenseApi.getIsActive)
// console.log(response.apiResult)
if (response) {
yield put(checkLicensePresentResponse(response.apiResult))
return
}
yield put(checkLicensePresentResponse(false))
} catch (error) {
console.log('Error in checking License present.', error)
Expand All @@ -49,18 +66,45 @@ function* activateLicenseWorker({ payload }) {
yield put(activateLicenseResponse())
}

function* activateCheckUserApi({ payload }) {
try {
const licenseApi = yield* getApiTokenWithDefaultScopes()
const response = yield call(licenseApi.submitApiKey, payload)
yield put(checkUserApiKeyResponse(response))
} catch (error) {
console.log(error)
}
}
function* activateCheckUserLicenseKey({ payload }) {
try {
const licenseApi = yield* getApiTokenWithDefaultScopes()
const response = yield call(licenseApi.submitLicenseKey, payload)
yield put(checkUserLicenseKeyResponse(response))
} catch (error) {
console.log(error)
}
}

//watcher sagas
export function* checkLicensePresentWatcher() {
yield takeEvery(CHECK_FOR_VALID_LICENSE, checkLicensePresentWorker)
yield takeEvery(ACTIVATE_CHECK_USER_LICENSE_KEY, activateCheckUserLicenseKey)
}

export function* activateLicenseWatcher() {
yield takeEvery(ACTIVATE_LICENSE, activateLicenseWorker)
}

export function* activateCheckApiKeyWatcher() {
yield takeEvery(ACTIVATE_CHECK_USER_API, activateCheckUserApi)
}
/**
* License Root Saga
*/
export default function* rootSaga() {
yield all([fork(checkLicensePresentWatcher), fork(activateLicenseWatcher)])
yield all([
fork(checkLicensePresentWatcher),
fork(activateLicenseWatcher),
fork(activateCheckApiKeyWatcher),
])
}
Loading

0 comments on commit eab2249

Please sign in to comment.