Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RMET-2252 :: Remove JSON file dependency , add Endpoint #38

Merged
merged 11 commits into from
Feb 16, 2023
110 changes: 51 additions & 59 deletions hooks/androidCopyPreferences.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,66 @@
const et = require('elementtree');
const path = require('path');
const fs = require('fs');
const { ConfigParser } = require('cordova-common');

const ProvidersEnum = Object.freeze({"apple":"1", "facebook":"2", "google":"3", "linkedIn":"4"})
const ApplicationTypeEnum = Object.freeze({"web":"1", "ios":"2", "android":"3"})
const {getJsonFile, ProvidersEnum, ApplicationTypeEnum} = require('./utils');

module.exports = function (context) {

const configFileName = 'www/json-config/SocialLoginsConfigurations.json';
var linkedin_deeplink_url = "";
var linkedin_deeplink_host = "";
var linkedin_deeplink_path = "";
var projectRoot = context.opts.cordova.project ? context.opts.cordova.project.root : context.opts.projectRoot;
module.exports = async function (context) {
let projectRoot = context.opts.cordova.project ? context.opts.cordova.project.root : context.opts.projectRoot;
let configXML = path.join(projectRoot, 'config.xml');
let configParser = new ConfigParser(configXML);
let configuratorURL = configParser.getGlobalPreference("SOCIAL_CONF_API_ENDPOINT");

if(configuratorURL.length == 0)
throw new Error("Missing preference: SOCIAL_CONF_API_ENDPOINT. Please make sure this preference is configured");

let appName = configParser.name();

let jsonConfig = await getJsonFile(configuratorURL, appName);
copyFacebookPreferences(jsonConfig, projectRoot);
copyLinkedInPreferences(jsonConfig, projectRoot);
};

copyFacebookPreferences(projectRoot);
function copyLinkedInPreferences(jsonConfig, projectRoot) {
let linkedin_deeplink_url = "";
let linkedin_deeplink_host = "";
let linkedin_deeplink_path = "";

//read json config file www/jsonConfig/sociallogins_configurations.json
var jsonConfig = "";
try {
jsonConfig = path.join(projectRoot, configFileName);
var jsonConfigFile = fs.readFileSync(jsonConfig).toString();
var jsonParsed = JSON.parse(jsonConfigFile);

linkedin_deeplink_url = jsonParsed.app_deeplink.url_scheme.replace(/\s/g, '')
linkedin_deeplink_host = jsonParsed.app_deeplink.url_host.replace(/\s/g, '')
linkedin_deeplink_path = jsonParsed.app_deeplink.url_path.replace(/\s/g, '')
linkedin_deeplink_url = jsonConfig.app_deeplink.url_scheme.replace(/\s/g, '')
linkedin_deeplink_host = jsonConfig.app_deeplink.url_host.replace(/\s/g, '')
linkedin_deeplink_path = jsonConfig.app_deeplink.url_path.replace(/\s/g, '')
} catch {
throw new Error("Missing configuration file or error trying to obtain the configuration.");
throw new Error("Error trying to obtain the configuration.");
}

//go inside the AndroidManifest and change values for schema, host and path
var manifestPath = path.join(projectRoot, 'platforms/android/app/src/main/AndroidManifest.xml');
var manifestFile = fs.readFileSync(manifestPath).toString();
var etreeManifest = et.parse(manifestFile);

var dataTags = etreeManifest.findall('./application/activity/intent-filter/data[@android:scheme="OAUTH_DEEPLINK_SCHEME"]');
for (var i = 0; i < dataTags.length; i++) {
var data = dataTags[i];
data.set("android:scheme", linkedin_deeplink_url);
}
let manifestPath = path.join(projectRoot, 'platforms/android/app/src/main/AndroidManifest.xml');
let manifestFile = fs.readFileSync(manifestPath).toString();
let etreeManifest = et.parse(manifestFile);

let dataTags = etreeManifest.findall('./application/activity/intent-filter/data[@android:scheme="OAUTH_DEEPLINK_SCHEME"]');
dataTags.forEach((data) => data.set("android:scheme", linkedin_deeplink_url));
dataTags = null;

dataTags = etreeManifest.findall('./application/activity/intent-filter/data[@android:host="OAUTH_DEEPLINK_HOST"]');
for (var i = 0; i < dataTags.length; i++) {
var data = dataTags[i];
data.set("android:host", linkedin_deeplink_host);
}

dataTags.forEach((data) => data.set("android:host", linkedin_deeplink_host));
dataTags = null;

dataTags = etreeManifest.findall('./application/activity/intent-filter/data[@android:path="OAUTH_DEEPLINK_PATH"]');
for (var i = 0; i < dataTags.length; i++) {
var data = dataTags[i];
data.set("android:path", linkedin_deeplink_path);
}
dataTags.forEach((data) => data.set("android:path", linkedin_deeplink_path));

var resultXmlManifest = etreeManifest.write();
let resultXmlManifest = etreeManifest.write();
fs.writeFileSync(manifestPath, resultXmlManifest);

};
}

function copyFacebookPreferences(projectRoot) {
function copyFacebookPreferences(jsonConfig, projectRoot) {

var facebook_client_appId = "";
var facebook_client_token = "";
let facebook_client_appId = "";
let facebook_client_token = "";

var jsonConfig = "";
try {
jsonConfig = path.join(projectRoot, 'www/json-config/SocialLoginsConfigurations.json');
var jsonConfigFile = fs.readFileSync(jsonConfig).toString();
var jsonParsed = JSON.parse(jsonConfigFile);

jsonParsed.app_configurations.forEach(function(configItem) {
jsonConfig.app_configurations.forEach( (configItem) =>{
if ((configItem.provider_id == ProvidersEnum.facebook) &&
(configItem.application_type_id == ApplicationTypeEnum.android)) {
facebook_client_appId = configItem.client_id;
Expand All @@ -81,21 +71,23 @@ function copyFacebookPreferences(projectRoot) {
throw new Error("Missing configuration file or error trying to obtain the configuration.");
}

var stringsPath = path.join(projectRoot, 'platforms/android/app/src/main/res/values/strings.xml');
var stringsFile = fs.readFileSync(stringsPath).toString();
var etreeStrings = et.parse(stringsFile);
let stringsPath = path.join(projectRoot, 'platforms/android/app/src/main/res/values/strings.xml');
let stringsFile = fs.readFileSync(stringsPath).toString();
let etreeStrings = et.parse(stringsFile);

var appIdTags = etreeStrings.findall('./string/[@name="facebook_app_id"]');
for (var tag of appIdTags) {
let appIdTags = etreeStrings.findall('./string/[@name="facebook_app_id"]');
for (let tag of appIdTags) {
tag.text = facebook_client_appId;
}

var clientTokenTags = etreeStrings.findall('./string/[@name="facebook_client_token"]');
for (var tag of clientTokenTags) {
let clientTokenTags = etreeStrings.findall('./string/[@name="facebook_client_token"]');
for (let tag of clientTokenTags) {
tag.text = facebook_client_token;
}

var resultXmlStrings = etreeStrings.write();
let resultXmlStrings = etreeStrings.write();
fs.writeFileSync(stringsPath, resultXmlStrings);

};


31 changes: 13 additions & 18 deletions hooks/iOSCopyPreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ const plist = require('plist');
const { ConfigParser } = require('cordova-common');
const { Console } = require('console');

module.exports = function (context) {
const {getJsonFile, ProvidersEnum, ApplicationTypeEnum} = require('./utils');

const ProvidersEnum = Object.freeze({"apple":"1", "facebook":"2", "google":"3", "linkedIn":"4"})
const ApplicationTypeEnum = Object.freeze({"web":"1", "ios":"2", "android":"3"})
module.exports = async function (context) {
var projectRoot = context.opts.cordova.project ? context.opts.cordova.project.root : context.opts.projectRoot;

var google_url_scheme = "";
Expand All @@ -19,22 +18,18 @@ module.exports = function (context) {
var deeplink_url_scheme = "";

var appNamePath = path.join(projectRoot, 'config.xml');
var appNameParser = new ConfigParser(appNamePath);
var appName = appNameParser.name();
var configParser = new ConfigParser(appNamePath);
var appName = configParser.name();

let platformPath = path.join(projectRoot, 'platforms/ios');
let configuratorURL = configParser.getGlobalPreference("SOCIAL_CONF_API_ENDPOINT");

if(configuratorURL.length == 0)
throw new Error("Missing preference: SOCIAL_CONF_API_ENDPOINT. Please make sure this preference is configured");

let jsonConfig = await getJsonFile(configuratorURL, appName);

//read json config file platforms/ios/www/jsonConfig
var jsonConfig = "";
try {
jsonConfig = path.join(projectRoot, 'www/json-config/SocialLoginsConfigurations.json');
var jsonConfigFile = fs.readFileSync(jsonConfig, 'utf8');
var jsonParsed = JSON.parse(jsonConfigFile);
} catch {
throw new Error("Missing configuration file or error trying to obtain the configuration.");
}

const iOSConfigArray = jsonParsed.app_configurations.filter(configItem => configItem.application_type_id == ApplicationTypeEnum.ios);
const iOSConfigArray = jsonConfig.app_configurations.filter(configItem => configItem.application_type_id == ApplicationTypeEnum.ios);
const errorMap = new Map();

iOSConfigArray.forEach(function(configItem) {
Expand Down Expand Up @@ -73,8 +68,8 @@ module.exports = function (context) {
}
});

if (jsonParsed.app_deeplink.url_scheme != null && jsonParsed.app_deeplink.url_scheme !== "") {
deeplink_url_scheme = jsonParsed.app_deeplink.url_scheme;
if (jsonConfig.app_deeplink.url_scheme != null && jsonConfig.app_deeplink.url_scheme !== "") {
deeplink_url_scheme = jsonConfig.app_deeplink.url_scheme;
} else {
errorMap['General'] = ['URL Scheme'];
}
Expand Down
32 changes: 32 additions & 0 deletions hooks/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const axios = require('axios');

module.exports = {
getJsonFile: async function getJsonFile(endpoint, appName){
try {
let response = await axios.get(endpoint, {
params: { AppName : appName }
});

let json = response.data;
return json;
} catch(err){
if(err.response){
if(err.response.status == 400){
let data = err.response.data;
if(data.Errors){
throw new Error(data.Errors);
} else throw new Error("Bad Request: make sure your apps is configured correctly")
}
if(err.response.status == 404){
throw new Error("Not found: Social Logins Configurator is either outdated or CONFIGURATOR_BASE_URL is not well defined.");
}
} else if(err.request){
throw new Error("Something went wrong with the request. " + err.toJSON());
} else{
throw new Error(err.message)
}
}
},
ProvidersEnum: Object.freeze({"apple":"1", "facebook":"2", "google":"3", "linkedIn":"4"}),
ApplicationTypeEnum: Object.freeze({"web":"1", "ios":"2", "android":"3"})
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"browser"
]
},
"engines": []
"engines": [],
"dependencies": {
"axios": "^1.3.3"
}
}