I have implemented Single Sign-On using node js in a Apache server. If I access any web application in the server, it would redirect to the sign-in page and after providing the credential, it is navigating to the web application. It is working well in all the browser. If I want to access the web application through the iOS app(WKWebView), I need to bypass sign-in page but load the web application straightaway as the authentication process are already done in the iOS app. But with this code, the web application is loading in the app but not displaying the content properly. Facing some parsing stylesheet error. Please find the attached screenshot.
Node JS code
const express = require('express');
const app = express();
var cookieParser = require('cookie-parser');
const port = 3000;
const axios = require('axios')
var useragent = require('express-useragent');
const authURL = 'https://**********.######.com/as/authorization.oauth2';
const tokenURL = 'https://**********.######.com/as/token.oauth2';
const upiURL = 'https://idp-d.######.com/upi/profile/';
const client_id = '$$$$$$$$';
const grant_type = 'authorization_code';
const response_type = 'code';
const scope = 'profile_email+profile_name+profile_sales+profile_location+profile_org+profile_network+profile_contact';
var loadURL = '';
app.use(cookieParser());
app.use(useragent.express());
var middleware = {
requireAuthentication : function(req, res, next) {
// res.send(req.useragent.isWebkit);
// res.send('testData');
const redirectURL = `https://${req.headers.host}`;
var urlString = '';
var code = req.query.code;
var headerValue = req.header('aofAccessToken'); // getting the token from iOS application
var getCookies = req.cookies.userOncoData;
if(headerValue)
{
next(); // if we have token from app, expecting to skip the authentication page and redirect to web application.
}
else
{
if(!getCookies) {
if (!code) {
loadURL = req.url;
urlString = `${authURL}?client_id=${client_id}&response_type=${response_type}&scope=${scope}&redirect_uri=${redirectURL}`;
return res.redirect(urlString);
} else {
axios({
// make a POST request
method: 'post',
url: `${tokenURL}?code=${req.query.code}&client_id=${client_id}&grant_type=${grant_type}&scope=${scope}&redirect_uri=${redirectURL}`,
// Set the content type header, so that we get the response in JSON
headers: {
accept: 'application/json'
}
}).then((response) => {
res.cookie('userOncoData', response.data.access_token, { maxAge: 300000, httpOnly: true })
//Step 3: Fetch User Data (Yet to Implement)
res.redirect(loadURL);
}).catch((error) => {
// Error
if (error.response) {
res.send("error.response" + error.response)
} else if (error.request) {
res.send("error.request" + error.request);
} else {
res.send('Error', error.message);
}
});
}
}
else {
next();
}
}
}
}
app.use(middleware.requireAuthentication); // If I comment this code, I am able to access the web application perfectly in both browser and iOS app but single sign-on feature is not working.
app.use(express.static(__dirname));
app.get('/', (req, res) => res.send('Data fetch has failed, Please check the URL and try again!'));
app.listen(0, () => console.log(`Example app listening on port ${port}!`));
Objective C code - Initiating WKWebView call
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
self.webLinkViewer = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
[self.webLinkViewer setFrame:CGRectMake(0, 142, self.view.frame.size.width, self.view.frame.size.height-142)];
self.webLinkViewer.navigationDelegate = self;
self.webLinkViewer.UIDelegate = self;
self.webLinkViewer.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:self.webLinkViewer];
[self loadWebPageWithURL:@"https://&&&&&&&&.&&&&.net/ppd/secure/ci/index.html" andWebview:self.webLinkViewer];
- (void)loadWebPageWithURL:(NSString *)urlString andWebview:(WKWebView *)webview
{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
if([urlString containsString:dlServerHostName])
{
NSString *accessToken = @”token”;
if(accessToken.length > 0)
{
[urlRequest setValue:accessToken forHTTPHeaderField:@”aofAccessToken”];
}
}
[webview loadRequest:urlRequest];
}
loadWebPageWithURL
, i.e.<link rel="stylesheet" type="text/css" href="style.css" />
? – Inger