Commit 9804b0b6 authored by Steve Johnson's avatar Steve Johnson

initial commit

parents
php/oauth2/tokens/**
php/oauth2/cookies/**
php/agents/**
QRCodeReader @ cf714698
Subproject commit cf7146980169c6deadb6ed69d4fbee932c1eddca
This diff is collapsed.
#My Stuff Everywhere™ - mse-usps repository
Contains a mockup of remote login application. This code is intended for illustration purposes and API validation only. You should use this as a functional guide and write your code from scratch.
A mobile QRCode application is used to scan a QRCode on a web page and be automatically logged in to that web page.
The main application web page is usps.html. This is the login page. On successful login, the user is redirected to php/smc.php.
The php folder contains a mockup of an oauth2 API for obtaining access tokens and executing a remote login using an access token.
There are several folders that need to exist with write permissions on the server for this to work:
yourdomain.com/prod/mse/usps/php/agents
yourdomain.com/prod/mse/usps/php/oauth2/tokens
yourdomain.com/prod/mse/usps/php/oauth2/cookies
<html>
<head>
<title>Token Exchange</title>
<meta name='copyright' content='CableLabs © 2014. All Rights Reserved'>
<meta name="viewport" charset="UTF-8" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta http-equiv="cache-control" content="no-cache">
<!-- stylesheets - external -->
<!-- stylesheets - local -->
<!-- javascript - external -->
<script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
<!-- javascript - local -->
<script type="text/javascript" src="../../ext/js/jquery.cookie.js" ></script>
<script type="text/javascript" src="../../js/utility.js" ></script>
<script type="text/javascript" src="../../js/xhr.js" ></script>
<script type="text/javascript" src="js/url.js" ></script>
</head>
<body class="en webkit">
<div id="msg"> </div>
<script>
$( document ).ready(function() {
//
var qp = queryParams();
if (qp.code == undefined) {
$("#msg").html("<h2>Missing token code</h2>");
}
else {
var request = new XMLHttpRequest();
var url = Url.token(qp.code)+"&grant_type=authorization_code";
var options = {method: "POST"};
request.body = "client_id=usps_id0123456789";
request.body += "&client_secret=usps_secret0123456789";
request.body += "&redirect_uri=http://localhost/mse/auth/user_portal.php";
//request.body = JSON.stringify(post);
request.issue(url, function(response){
if (response.httpStatus === 200) {
$("#msg").html("Received token: "+response.responseText);
}
else {
$("#msg").html("Failed to retrieve token for code: "+qp.code+" Error: "+response.responseText);
}
}, options);
}
});
</script>
</body>
</html>
/* url.js
*
* url resolution methods
*
* Copyright (c) 2014 CableLabs. All rights reserved.
*
* There is one constant url - <location>/auth/config.json.
* This can contain overrides for any of the other urls used by the system.
* Otherwise, the defaults are constructed below.
*
* Bridge URLs must be absolute if specified in config.json, including the protocol (ws://, wss://) and port
*
* Note: This currently only supports port 80/443 for http/https
*/
var Url = (function() {
////////////////////////////////////
// public
////////////////////////////////////
var obj = {};
////////////////////////////////////
// private
////////////////////////////////////
var config = {};
// Get the path for this script file, which is in the js folder under the root folder for this project, and determine parent directory (baseUrl)
var scriptEls = document.getElementsByTagName( 'script' );
var thisScriptEl = scriptEls[scriptEls.length - 1];
var scriptPath = thisScriptEl.src;
var scriptFolder = scriptPath.substr(0, scriptPath.lastIndexOf( '/' ));
var baseUrl = scriptFolder.substr(0, scriptFolder.lastIndexOf( '/' )+1);
var defaultBase = document.location.protocol + "//" + document.location.host;
defaultBase += "/atg/mse/qrc/";
////////////////////////////////////
// fetch default configuration
////////////////////////////////////
var authURL = baseUrl + "config/config.json";
var request = new XMLHttpRequest();
request.issue(authURL, function(response){
if (response.httpStatus === 200) {
try {
config = JSON.parse(response.responseText);
}
catch(e) {
}
}
});
////////////////////////////////////
// private methods
////////////////////////////////////
function resolveURL(name, defaultURL, rootUrl) {
if (rootUrl == undefined) {
rootUrl = baseUrl;
}
if (config[name]) {
if (config[name].indexOf("http") === 0) {
// Absolute url
return config[name];
}
else {
// relative url
return rootUrl + config[name];
}
}
else {
return rootUrl + defaultURL;
}
}
////////////////////////////////////
// url resolvers
// Note: Because we can now redirect to a different root (so we can develop separate from production),
// all of the auth functions still go to the default base url, otherwise we won't find the cookie records.
////////////////////////////////////
obj.remoteLoginAgent = function(connectCode) {
return resolveURL("remoteLoginAgent", "php/remote_login_agent.php", defaultBase)+"?connectCode="+connectCode;
};
// Used by TokenExchange.html (USPS)
obj.token = function(connectCode) {
return resolveURL("token", "php/oauth2/token.php")+"?code="+connectCode;
};
obj.genQRCode = function() {
return resolveURL("loginService", "php/gen_qrcode.php");
};
obj.secureMessageCenter = function() {
return resolveURL("secureMessageCenter", "php/smc.php");
};
obj.authorize = function() {
return resolveURL("remoteAuthenticate", "php/oauth2/authorize.php");
}
obj.remoteAuthentication = function() {
return resolveURL("remoteAuthenticate", "php/remote_authenticate.php");
}
obj.relative = function(url) {
return baseUrl+url;
}
obj.baseUrl = function() {
return baseUrl;
}
obj.defaultBase = function() {
return defaultBase;
}
obj.rebaseCurrent = function(newBase) {
var current = location.href;
var relative = current.replace(obj.baseUrl(), "");
return newBase + relative;
}
obj.loginSettings = function(redirectUri) {
//return resolveURL("loginSettings", "remote/loginSettings.html?redirect_uri="+redirectUri, settingsBase);
return resolveURL("loginSettings", "remote/loginSettings.html?redirect_uri="+redirectUri);
};
return obj;
}());
/* Copyright © CableLabs 2013. All Rights Reserved */
/*
remotecontainerlogin.css
*/
html {
width: 100%;
height: 100%;
overflow: hidden;
}
body {
overflow: hidden;
}
.hidden {
opacity: 0;
}
#home {
min-height: 100%;
height: 100%;
}
.input {
width: 100%;
max-width: 400px;
}
#usernameLabel, #passwordLabel {
width: 100%;
max-width: 400px;
margin: 10px auto;
text-align: left;
display: inline-block;
}
#username, #password, #loginButton {
max-width: 400px;
}
#hostLabel {
color: #606060;
}
<!DOCTYPE html>
<html>
<head>
<meta name='copyright' content='CableLabs © 2014. All Rights Reserved'>
<title>QRC Login</title>
<meta name="viewport" charset="UTF-8" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="cache-control" content="no-cache">
<script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link href="login.css" media="screen" rel="stylesheet" type="text/css"/>
<script src="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<!-- javascript - external -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" src="../../../sdom/ext/js/jquery.mobile.simpledialog2.min.js"></script>
<!-- javascript - local -->
<script type="text/javascript" src="../../../sdom/js/constants.js" ></script>
<script type="text/javascript" src="../../../sdom/js/base64.js" ></script>
<script type="text/javascript" src="../../../sdom/js/xhr.js" ></script>
<script type="text/javascript" src="../../../sdom/js/utility.js" ></script>
<script type="text/javascript" src="../../js/url.js" ></script>
<script type="text/javascript" src="../../js/auth.js" ></script>
<script type="text/javascript" src="login.js" ></script>
</head>
<body onload="initLogin();" >
<!--div id='home' data-theme="e"-->
<div id="home" data-role="page" data-title="Remote Login">
<!--div data-role="header" data-theme="e">
<h1>Remote Menu</h1>
</div-->
<header data-role="header" data-position="fixed"><h1>Remote Login</h1>
<a href="#login settings" data-role="button" data-icon="gear" data-iconshadow="false"
data-direction="reverse" onclick="loginSettings(); return false;" data-transition="slide"
data-iconpos="notext" class="ui-btn-right">login settings</a>
</header>
<div data-role="content" data-theme='a' id="home">
<div data-role="fieldcontain" data-theme='a' id="loginpanel" style='font-family: verdana; font-size: 14px;'>
<!-- cablelabs logo image -->
<!--br-->
<!--center><img id='logoimage' src='../../images/cablelabs_logo_about.jpg' style='border: 3px #3168B2 solid;' onclick='prefill();'/></center-->
<!-- input fields -->
<div id='inputfields' style='margin: 24px;'>
<center>
<label id="usernameLabel">
Username:
</label>
</center>
<center>
<input type='text' id='username' style='width: 100%;' autofocus='autofocus' autocapitalize='off' autocorrect='off'
onchange='onInputTextChange();' onkeypress='this.onchange();' onpaste='this.onchange();' oninput='this.onchange();'>
</center>
<br>
<center>
<label id="passwordLabel">
Password:
</label>
</center>
<center>
<input type='password' id='password' style='width: 100%;' autocapitalize='off' autocorrect='off'
onchange='onInputTextChange();' onkeypress='this.onchange();' onpaste='this.onchange();' oninput='this.onchange();'>
</center>
</div>
<!-- login button -->
<a data-role='button' id='loginButton' style='width: 60%; margin: auto; text-align: center;' onclick='clickLoginButton(); return false;'href='#'>Login</a>
<br>
<center><label id="hostLabel">hostname</label></center>
<br>
<br>
<br>
</div>
<br><br>
<br><br>
</div>
</div>
</body>
</html>
/////////////////////////////////////////////////////////////////////////////////
//
// login.js
//
// SharedDOM prototype app
//
// Handles the remote login page.
//
// Copyright (c) 2014 CableLabs. All rights reserved.
//
/////////////////////////////////////////////////////////////////////////////////
var EAlertType_Success = 0;
var EAlertType_Info = 1;
var EAlertType_Warning = 2;
var EAlertType_Error = 3;
function initLogin() {
sizeRemotePage();
window.addEventListener('keyup', function(event) {
if(event.keyCode == 13){
clickLoginButton();
}
}, true);
var baseurl = document.location.protocol + "//" + document.location.host + "/" + document.location.pathname.split("/")[1];
$('#hostLabel').text(baseurl);
}
function prefill() {
document.getElementById('username').value = 'demo';
document.getElementById('password').value = 'demo';
}
function clickLoginButton() {
var KAppMsg_LoginFailed = "Login Failed";
var KAppMsg_InvalidLogin = "Incorrect username or password.";
var KAppMsg_LoggingIn = "logging in...";
var KAppMsg_LoginTitle = "Login";
var KAppMsg_Initializing = "Initializing...";
var KAppMsg_EnterValidUsername = "Please enter a valid username.";
var KAppMsg_EnterValidPassword = "Please enter a valid password.";
//console.log("clickLoginButton()");
var username, password;
var result;
// get the username and password values
username = document.getElementById('username').value;
if (username === '') {
showAlert(KAppMsg_LoginTitle, KAppMsg_EnterValidUsername, EAlertType_Info);
document.getElementById('username').focus();
return;
}
password = document.getElementById('password').value;
if (password === '') {
showAlert(KAppMsg_LoginTitle, KAppMsg_EnterValidPassword, EAlertType_Info);
document.getElementById('password').focus();
return;
}
console.log("username: " + username);
console.log("password: " + password);
showLoading(true, "logging in...");
// login
Auth.loginUser(username, password, function(response){
showLoading(false, "");
if (response.error) {
showAlert("Login Failed", response.error, EAlertType_Error);
}
else if (response.httpStatus == 200) {
// Call iOS
//alert("login successful");
var url = "msejsb://login";
window.location.replace(url);
}
else if (response.httpStatus == 401) {
// show the user the login failure
showAlert("Login Failed", "Incorrect username or password.", EAlertType_Error);
}
else {
console.log("unknown status: " + response.httpStatus);
}
});
}
function remoteLogin(metadata) {
console.log("remoteLogin: "+JSON.stringify(metadata));
var request = new XMLHttpRequest();
var url = Url.remoteLogin()+"?ccode="+metadata.connectCode+"&serviceid="+metadata.serviceProvider;
request.issue(url, function(response){
if (response.httpStatus === 200) {
}
else {
// Check for 401 here.
var msg = response.error ? response.error : "HTTP 1.0 "+response.httpStatus;
//console.log("Failed to retrieve Session Token - "+ msg + " - "+response.responseText);
// Go to support page??
}
});
}
function showAlert(title, message, alertType) {
// use the SimpleDialog object to show the about box
// http://dev.jtsage.com/jQM-SimpleDialog/demos2/index.html
// set a default title if not present
if (title == '') {
title = "alert";
}
var alertImage = "";
var alertImageTag = "";
var messageHTML = "";
// select the alert icon based on the alert type
switch(alertType) {