-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial, after separation from twiz-client source
- Loading branch information
0 parents
commit ebc52c5
Showing
6 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules/ | ||
.cache/ | ||
.nyc_output/ | ||
coverage/ | ||
*instrumented* | ||
*bundle* | ||
*coverage* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: node_js | ||
node_js: | ||
- '8.6.0' | ||
git: | ||
depth: 3 | ||
before_install: | ||
- 'sudo apt-get update && sudo apt-get install linux-image-generic ' | ||
after_success: 'npm run coveralls' | ||
cache: | ||
directiories: | ||
- 'node_modules' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Mocha Tests</title> | ||
<link href="node_modules/mocha.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<div id="mocha"></div> | ||
|
||
<script src="node_modules/mocha/mocha.js"></script> | ||
|
||
<script>mocha.setup('bdd')</script> | ||
<script src="test/accesstoken_bundle.js"></script> | ||
<script> | ||
mocha.checkLeaks(); | ||
mocha.run(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "twiz-client-accesstoken", | ||
"version": "1.0.0", | ||
"description": "Access Token leg of OAuth 1.0 a for twiz-client", | ||
"main": "src/AccessToken.js", | ||
"scripts": { | ||
"lint": "eslint src/AccessToken.js", | ||
"instrument": "istanbul instrument src/AccessToken.js --output src/AccessToken_instrumented.js", | ||
"browserify": "./node_modules/browserify/bin/cmd.js test/accesstoken.js -o test/accesstoken_bundle.js", | ||
"mocha-headless": "mocha-headless-chrome -f mocha-test.html -c test/coverage.json", | ||
"report": "istanbul report --root test/ lcov", | ||
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"test": "npm run instrument && npm run browserify && npm run mocha-headless && npm run report" | ||
}, | ||
"author": "github.com/gits2501", | ||
"license": "MIT", | ||
"dependencies": { | ||
"twiz-client-oauth": "file:../twiz-client-oauth", | ||
"twiz-client-redirect": "file:../twiz-client-redirect" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^16.2.0", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.19.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.1.1", | ||
"mocha-headless-chrome": "^2.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
var OAuth = require('twiz-client-oauth'); | ||
var deliverData = require('twiz-client-redirect').prototype.deliverData; | ||
|
||
function AccessToken (){ // checks that oauth data is in redirection(callback) url, and makes sure | ||
// that oauth_token from url matches the one we saved in first step. | ||
OAuth.call(this); | ||
this.name = this.leg[2]; | ||
|
||
this.redirectionUrlParsed; // redirection(callback) url parsing status | ||
this.redirectionData; // parsed data from redirection url | ||
|
||
this.loadedRequestToken; // place to load token | ||
this.authorized; // redirection data that was autorized; | ||
this.winLoc = window.location.href; // get current url | ||
|
||
this.addCustomErrors({ // add error messages related to this module | ||
verifierNotFound: '"oauth_verifier" string was not found in redirection(callback) url.', | ||
tokenNotFound: '"oauth_token" string was not found in redirection(callback) url.', | ||
tokenMissmatch: 'Request token and token from redirection(callback) url do not match', | ||
requestTokenNotSet: 'Request token was not set', | ||
requestTokenNotSaved: 'Request token was not saved. Check that page url from which you make request match your redirection_url.', | ||
noRepeat: "Cannot make another request with same redirection(callback) url", | ||
noStringProvided: "Expected string was not provided" | ||
}) | ||
} | ||
|
||
AccessToken.prototype = Object.create(OAuth.prototype); | ||
|
||
AccessToken.prototype.setAuthorizedTokens = function(){ | ||
|
||
this.authorizeRedirectionUrl(), | ||
// set params for access token leg explicitly | ||
this.oauth[this.prefix + 'verifier'] = this.authorized.oauth_verifier // Put authorized verifier | ||
this.oauth[this.prefix + 'token'] = this.authorized.oauth_token; // Authorized token | ||
} | ||
|
||
AccessToken.prototype.authorizeRedirectionUrl = function(){// makes sure we have needed data in redirection url | ||
this.parseRedirectionUrl(this.winLoc); // parse | ||
return this.authorize(this.redirectionData); // authorize token | ||
|
||
} | ||
|
||
AccessToken.prototype.parseRedirectionUrl = function(url){ // parses data in url | ||
// console.log('in parseRedirectionUrl'); | ||
|
||
var str = this.parse(url, /\?/g, /#/g); // parses query string | ||
this.redirectionData = this.parseQueryParams(str); // parse parameters from query string | ||
|
||
this.redirectionUrlParsed = true; // indicate that the url was already parsed | ||
|
||
// console.log(this.redirectionData.twiz_); | ||
} | ||
|
||
AccessToken.prototype.parse = function(str, delimiter1, delimiter2){ // parses substring of a string (str) | ||
|
||
if(!str) throw this.CustomError('noStringProvided'); | ||
|
||
var start = str.search(delimiter1); // calculate from which index to take | ||
var end ; | ||
if(!delimiter2 || str.search(delimiter2) === -1) end = str.length;// if del2 was not passed as argument | ||
// or we didnt find it, then end index | ||
// is length of the string. | ||
else end = str.search(delimiter2); // calcualte to which index to take | ||
// console.log(str); | ||
return str.substring(start, end); // return substring | ||
|
||
}; | ||
|
||
|
||
AccessToken.prototype.parseQueryParams = function (str){ | ||
var arr = []; | ||
if(!str) throw this.CustomError('noStringProvided'); | ||
|
||
|
||
if(str[0] === "?") str = str.substring(1); // remove "?" if we have one at beggining | ||
|
||
arr = str.split('&') // make new array element on each "&" | ||
.map( function(el, i){ | ||
var arr2 = el.split("="); // for each element make new array element on each "=" | ||
return arr2; | ||
|
||
}); | ||
|
||
// console.log(arr); | ||
return this.objectify(arr); // makes an object from query string parametars | ||
} | ||
|
||
AccessToken.prototype.objectify = function(array){// makes new object with props and values from array's | ||
// elements | ||
var data = {}; | ||
var len = array.length; | ||
|
||
for(var i = 0; i < len; i++){ | ||
var arr = array[i]; | ||
for(var j = 0; j < arr.length; j++){ // iterating though each of arrays in parsed | ||
if(j == 0) data[arr[j]] = arr[j+1]; // if we are at element that holds name of property, | ||
// make property with that name in data object, set it's | ||
// value of next element (j+1) | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
AccessToken.prototype.authorize = function(sent){ // check that sent data from redirection url has needed info | ||
|
||
if(this.isRequestTokenUsed(window.localStorage)) | ||
throw this.CustomError('noRepeat'); | ||
|
||
|
||
// console.log('in authorize') | ||
if(!sent.oauth_verifier) throw this.CustomError('verifierNotFound'); | ||
if(!sent.oauth_token) throw this.CustomError('tokenNotFound'); | ||
|
||
this.loadRequestToken(window.localStorage, sent); // load token from storage | ||
|
||
// check that tokens match | ||
if(sent.oauth_token !== this.loadedRequestToken) throw this.CustomError('tokenMissmatch'); | ||
|
||
return this.authorized = sent; // data passed checks, so its authorized; | ||
} | ||
|
||
AccessToken.prototype.isRequestTokenUsed = function(storage){ // check that we have a token to use | ||
|
||
if(storage.requestToken_ === "null") return true; // token whould be "null" only when loadRequestToken() | ||
// run twice on same redirection(callback) url | ||
return false; | ||
} | ||
|
||
|
||
|
||
AccessToken.prototype.loadRequestToken = function(storage, sent){ | ||
|
||
if(!storage.hasOwnProperty('requestToken_')) throw this.CustomError('requestTokenNotSaved'); | ||
|
||
this.loadedRequestToken = storage.requestToken_; // load token from storage | ||
|
||
// console.log('storage after: ', storage.requestToken_); | ||
// console.log('this.loadedRequestToken :', this.loadedRequestToken); | ||
|
||
storage.requestToken_ = null; // since we've loaded the token, mark it as | ||
// used/erased with null | ||
// console.log('after erasing storage.requestToken :', storage.requestToken_); | ||
|
||
if (!this.loadedRequestToken) throw this.CustomError('requestTokenNotSet'); | ||
} | ||
|
||
AccessToken.prototype.getSessionData = function(){ // gets session data from redirection url | ||
console.log('in getSessionData') | ||
if(!this.redirectionUrlParsed); | ||
this.parseRedirectionUrl(window.location.href); // parse data from url | ||
|
||
if(!this.redirectionData.data){ // return if no session data | ||
console.log(this.messages.noSessionData); | ||
return; | ||
} | ||
|
||
this.sessionData = this.parseSessionData(this.redirectionData.data) // further parsing of session data | ||
console.log(this.sessionData); | ||
return this.sessionData; | ||
} | ||
|
||
AccessToken.prototype.parseSessionData = function(str){ | ||
if(/%[0-9][0-9]/g.test(str)) // See if there are percent encoded chars | ||
str = decodeURIComponent(decodeURIComponent(str)); // Decoding twice, since it was encoded twice | ||
// (by OAuth 1.0a specification). See genSBS function. | ||
return this.parseQueryParams(str); // Making an object from parsed key/values. | ||
} | ||
|
||
AccessToken.prototype.deliverData = deliverData; // borrow function from Redirect module | ||
|
||
module.exports = AccessToken; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var AccessToken = require('../src/AccessToken_instrumented'); | ||
var assert = require('assert'); | ||
|
||
|
||
|
||
describe('Access Token', function(){ | ||
|
||
|
||
describe('Access Token leg', function(){ | ||
var request_token = 'longStringOfAlphanumerics33521' // mock request token from first leg (request token leg); | ||
var query = '?oauth_token='+request_token+'&oauth_verifier=similarStringOfAlphanumerics4224'; // make query string | ||
window.localStorage.requestToken_ = request_token // mock saved request token (in request token leg) | ||
|
||
var at = new AccessToken(); // make instance | ||
at.winLoc += query // mock authorized url (query string from twitter) | ||
|
||
it('ready ', function(){ | ||
assert.doesNotThrow(at.setAuthorizedTokens.bind(at)) | ||
}) | ||
}) | ||
|
||
describe('not ready') | ||
|
||
}) |