From 8f72c8c8fac3beef7a0823e467814ca5f76bccb4 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 21 Sep 2023 10:25:01 +0200 Subject: [PATCH] feat(5g-api): Introduce 5G api --- .../actions/get-device-location/index.ts | 61 +++++++++++++++++++ .../backend/src/apps/5g-api/actions/index.ts | 3 + .../src/apps/5g-api/assets/favicon.svg | 4 ++ .../backend/src/apps/5g-api/auth/index.ts | 45 ++++++++++++++ .../apps/5g-api/auth/verify-credentials.ts | 39 ++++++++++++ packages/backend/src/apps/5g-api/index.d.ts | 0 packages/backend/src/apps/5g-api/index.ts | 18 ++++++ .../backend/src/apps/high-mobility/index.d.ts | 0 8 files changed, 170 insertions(+) create mode 100644 packages/backend/src/apps/5g-api/actions/get-device-location/index.ts create mode 100644 packages/backend/src/apps/5g-api/actions/index.ts create mode 100644 packages/backend/src/apps/5g-api/assets/favicon.svg create mode 100644 packages/backend/src/apps/5g-api/auth/index.ts create mode 100644 packages/backend/src/apps/5g-api/auth/verify-credentials.ts create mode 100644 packages/backend/src/apps/5g-api/index.d.ts create mode 100644 packages/backend/src/apps/5g-api/index.ts create mode 100644 packages/backend/src/apps/high-mobility/index.d.ts diff --git a/packages/backend/src/apps/5g-api/actions/get-device-location/index.ts b/packages/backend/src/apps/5g-api/actions/get-device-location/index.ts new file mode 100644 index 00000000..cb61bcb6 --- /dev/null +++ b/packages/backend/src/apps/5g-api/actions/get-device-location/index.ts @@ -0,0 +1,61 @@ +import defineAction from '../../../../helpers/define-action'; + +export default defineAction({ + name: 'Get Device Location', + key: 'getDeviceLocation', + description: 'Get the location of the device.', + arguments: [ + { + label: 'MSISDN', + key: 'msisdn', + type: 'string' as const, + required: true, + description: + 'Subscriber number in E.164 format (starting with country code). Optionally prefixed with ' + + '.', + variables: true, + }, + { + label: 'Latitude', + key: 'latitude', + type: 'string' as const, + required: true, + description: 'Latitude component of location', + variables: true, + }, + { + label: 'Longitude', + key: 'longitude', + type: 'string' as const, + required: true, + description: 'Longitude component of location', + variables: true, + }, + { + label: 'Accuracy', + key: 'accuracy', + type: 'string' as const, + required: true, + description: 'Accuracy expected for location verification in km', + variables: true, + }, + ], + + async run($) { + const payload = { + ueId: { + msisdn: $.step.parameters.msisdn, + }, + latitude: $.step.parameters.latitude, + longitude: $.step.parameters.longitude, + accuracy: $.step.parameters.accuracy, + }; + + const response = await $.http.post( + 'https://api-eu.vonage.com/camara/location/v0/verify', + payload + ); + + $.setActionItem({ raw: response.data }); + }, +}); diff --git a/packages/backend/src/apps/5g-api/actions/index.ts b/packages/backend/src/apps/5g-api/actions/index.ts new file mode 100644 index 00000000..5e6b3327 --- /dev/null +++ b/packages/backend/src/apps/5g-api/actions/index.ts @@ -0,0 +1,3 @@ +import getDeviceLocation from './get-device-location'; + +export default [getDeviceLocation]; diff --git a/packages/backend/src/apps/5g-api/assets/favicon.svg b/packages/backend/src/apps/5g-api/assets/favicon.svg new file mode 100644 index 00000000..c3e61fc2 --- /dev/null +++ b/packages/backend/src/apps/5g-api/assets/favicon.svg @@ -0,0 +1,4 @@ + + + + diff --git a/packages/backend/src/apps/5g-api/auth/index.ts b/packages/backend/src/apps/5g-api/auth/index.ts new file mode 100644 index 00000000..c2ed0f47 --- /dev/null +++ b/packages/backend/src/apps/5g-api/auth/index.ts @@ -0,0 +1,45 @@ +import verifyCredentials from './verify-credentials'; +// import isStillVerified from './is-still-verified'; +// import refreshToken from './refresh-token'; + +export default { + fields: [ + { + key: 'screenName', + label: 'Screen Name', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: + 'Screen name of your connection to be used on Automatisch UI.', + clickToCopy: false, + }, + { + key: 'privateKey', + label: 'Private Key', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: 'Private Key of your High Mobility OAuth app.', + clickToCopy: false, + }, + { + key: 'applicationId', + label: 'Application ID', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: 'App ID of your High Mobility OAuth app.', + clickToCopy: false, + }, + ], + verifyCredentials, + // isStillVerified, + // refreshToken, +}; diff --git a/packages/backend/src/apps/5g-api/auth/verify-credentials.ts b/packages/backend/src/apps/5g-api/auth/verify-credentials.ts new file mode 100644 index 00000000..38a4cbd7 --- /dev/null +++ b/packages/backend/src/apps/5g-api/auth/verify-credentials.ts @@ -0,0 +1,39 @@ +import { IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; +import jwt from 'jsonwebtoken'; +import { v4 as uuidv4 } from 'uuid'; + +const verifyCredentials = async ($: IGlobalVariable) => { + const claims = { + application_id: $.auth.data.applicationId as string, + iat: Math.floor(new Date().getTime() / 1000), + exp: Math.floor(new Date().getTime() / 1000) + 24 * 60 * 60, + jti: uuidv4(), + }; + + const privateKey = ($.auth.data.privateKey as string).replaceAll('\\n', '\n'); + + const jwtToken = jwt.sign(claims, privateKey, { + algorithm: 'RS256', + }); + + const headers = { + Authorization: 'Bearer ' + jwtToken, + }; + + const response = await $.http.post( + 'https://api.nexmo.com/oauth2/token?grant_type=client_credentials', + null, + { headers } + ); + + const responseData = Object.fromEntries(new URLSearchParams(response.data)); + + await $.auth.set({ + accessToken: responseData.access_token, + tokenType: responseData.token_type, + expiresIn: responseData.expires_in, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/5g-api/index.d.ts b/packages/backend/src/apps/5g-api/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/backend/src/apps/5g-api/index.ts b/packages/backend/src/apps/5g-api/index.ts new file mode 100644 index 00000000..a33e9bc7 --- /dev/null +++ b/packages/backend/src/apps/5g-api/index.ts @@ -0,0 +1,18 @@ +import defineApp from '../../helpers/define-app'; +// import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; +import actions from './actions'; + +export default defineApp({ + name: '5G API', + key: '5g-api', + iconUrl: '{BASE_URL}/apps/5g-api/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/5g-api/connection', + supportsConnections: true, + baseUrl: 'https://developer.telekom.de', + apiBaseUrl: 'https://api.developer.telekom.de', + primaryColor: 'e20074', + // beforeRequest: [addAuthHeader], + auth, + actions, +}); diff --git a/packages/backend/src/apps/high-mobility/index.d.ts b/packages/backend/src/apps/high-mobility/index.d.ts new file mode 100644 index 00000000..e69de29b