├── .gitignore ├── LICENSE.md ├── README.md ├── app.py ├── pem ├── index.js └── package.json ├── requirements.txt ├── static ├── amazon-cognito-identity.min.js ├── app.js └── aws-cognito-sdk.min.js └── templates ├── code_registration.html ├── index.html └── welcome.html /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Bojan Baltic 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cognito-js-flask-tutorial 2 | 3 | #### Video tutorial 4 | https://www.youtube.com/watch?v=qMtk4LJ5OfE&list=PL7_Pg81ReB0yiKe1MHln4uxZ6GHv8W5uk
5 |
6 | #### Install dependencies 7 | Python:
8 | pip install -r requirements.txt 9 |

10 | Node.js:
11 | cd pem/
12 | npm install 13 | #### Start project 14 | python app.py

15 | Don't forget to fill your
16 | UserPoolId (static/app.js),
17 | ClientId (static/app.js),
18 | pem (app.py) and
19 | url (pem/index.js) 20 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request 2 | import jwt 3 | from jwt.contrib.algorithms.pycrypto import RSAAlgorithm 4 | 5 | app = Flask(__name__) 6 | 7 | jwt.register_algorithm('RS256', RSAAlgorithm(RSAAlgorithm.SHA256)) 8 | 9 | 10 | def is_token_valid(token): 11 | pems_dict = { 12 | 'kid1': 'pem1', 13 | 'kid2': 'pem2' 14 | } 15 | 16 | kid = jwt.get_unverified_header(token)['kid'] 17 | pem = pems_dict.get(kid, None) 18 | 19 | if pem is None: 20 | print 'kid false' 21 | return False 22 | 23 | try: 24 | decoded_token = jwt.decode(token, pem, algorithms=['RS256']) 25 | iss = 'https://cognito-idp.us-east-2.amazonaws.com/us-east-2_I1ZQrSsWb' 26 | if decoded_token['iss'] != iss: 27 | print 'iss false' 28 | return False 29 | elif decoded_token['token_use'] != 'access': 30 | print 'access false' 31 | return False 32 | return True 33 | except Exception: 34 | return False 35 | 36 | 37 | @app.route("/") 38 | def hello(): 39 | return render_template('index.html') 40 | 41 | 42 | @app.route("/code_registration") 43 | def code_registration(): 44 | return render_template('code_registration.html') 45 | 46 | 47 | @app.route("/welcome") 48 | def welcome(): 49 | return render_template('welcome.html') 50 | 51 | 52 | @app.route("/api/protected_api", methods=["POST"]) 53 | def protected_api(): 54 | access_token = request.form['access_token'] 55 | if (is_token_valid(access_token)): 56 | return 'some protected data from api' 57 | else: 58 | return 'bad token', 401 59 | 60 | 61 | if __name__ == '__main__': 62 | app.run(debug=True) 63 | -------------------------------------------------------------------------------- /pem/index.js: -------------------------------------------------------------------------------- 1 | var request = require('request'); 2 | var jwkToPem = require('jwk-to-pem'); 3 | 4 | //url: 'https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json' 5 | request({ 6 | url: 'https://cognito-idp.us-east-2.amazonaws.com/us-east-2_I1ZQrSsWb/.well-known/jwks.json', 7 | json: true 8 | }, function (error, response, body) { 9 | if (!error && response.statusCode === 200) { 10 | pems = {}; 11 | var keys = body['keys']; 12 | for(var i = 0; i < keys.length; i++) { 13 | //Convert each key to PEM 14 | var key_id = keys[i].kid; 15 | var modulus = keys[i].n; 16 | var exponent = keys[i].e; 17 | var key_type = keys[i].kty; 18 | var jwk = { kty: key_type, n: modulus, e: exponent}; 19 | var pem = jwkToPem(jwk); 20 | pems[key_id] = pem; 21 | } 22 | console.log(pems); 23 | } else { 24 | console.error("error"); 25 | } 26 | }); 27 | -------------------------------------------------------------------------------- /pem/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pem", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "author": "", 9 | "license": "ISC", 10 | "dependencies": { 11 | "jwk-to-pem": "^1.2.6", 12 | "request": "^2.83.0" 13 | }, 14 | "devDependencies": {}, 15 | "description": "" 16 | } 17 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | click==6.7 2 | Flask==0.12.2 3 | itsdangerous==0.24 4 | Jinja2==2.10 5 | MarkupSafe==1.0 6 | pkg-resources==0.0.0 7 | pycrypto==2.6.1 8 | PyJWT==1.5.3 9 | Werkzeug==0.14.1 10 | -------------------------------------------------------------------------------- /static/amazon-cognito-identity.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Copyright 2016 Amazon.com, 3 | * Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Licensed under the Amazon Software License (the "License"). 6 | * You may not use this file except in compliance with the 7 | * License. A copy of the License is located at 8 | * 9 | * http://aws.amazon.com/asl/ 10 | * 11 | * or in the "license" file accompanying this file. This file is 12 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, express or implied. See the License 14 | * for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("aws-sdk/global"),require("aws-sdk/clients/cognitoidentityserviceprovider")):"function"==typeof define&&define.amd?define(["aws-sdk/global","aws-sdk/clients/cognitoidentityserviceprovider"],t):"object"==typeof exports?exports.AmazonCognitoIdentity=t(require("aws-sdk/global"),require("aws-sdk/clients/cognitoidentityserviceprovider")):e.AmazonCognitoIdentity=t(e.AWSCognito,e.AWSCognito.CognitoIdentityServiceProvider)}(this,function(e,t){return function(e){function t(i){if(n[i])return n[i].exports;var s=n[i]={exports:{},id:i,loaded:!1};return e[i].call(s.exports,s,s.exports,t),s.loaded=!0,s.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function i(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t.default=e,t}function s(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0;var o=n(17);Object.keys(o).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return o[e]}})});var r=n(13),a=s(r),u=i(o);Object.keys(u).forEach(function(e){a.default[e]=u[e]})},function(t,n){t.exports=e},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}t.__esModule=!0;var o=n(1),r=n(3),a=i(r),u="FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF",c="userAttributes.",l=function(){function e(t){s(this,e),this.N=new a.default(u,16),this.g=new a.default("2",16),this.k=new a.default(this.hexHash("00"+this.N.toString(16)+"0"+this.g.toString(16)),16),this.smallAValue=this.generateRandomSmallA(),this.getLargeAValue(function(){}),this.infoBits=new o.util.Buffer("Caldera Derived Key","utf8"),this.poolName=t}return e.prototype.getSmallAValue=function(){return this.smallAValue},e.prototype.getLargeAValue=function(e){var t=this;this.largeAValue?e(null,this.largeAValue):this.calculateA(this.smallAValue,function(n,i){n&&e(n,null),t.largeAValue=i,e(null,t.largeAValue)})},e.prototype.generateRandomSmallA=function(){var e=o.util.crypto.lib.randomBytes(128).toString("hex"),t=new a.default(e,16),n=t.mod(this.N);return n},e.prototype.generateRandomString=function(){return o.util.crypto.lib.randomBytes(40).toString("base64")},e.prototype.getRandomPassword=function(){return this.randomPassword},e.prototype.getSaltDevices=function(){return this.SaltToHashDevices},e.prototype.getVerifierDevices=function(){return this.verifierDevices},e.prototype.generateHashDevice=function(e,t,n){var i=this;this.randomPassword=this.generateRandomString();var s=""+e+t+":"+this.randomPassword,r=this.hash(s),u=o.util.crypto.lib.randomBytes(16).toString("hex");this.SaltToHashDevices=this.padHex(new a.default(u,16)),this.g.modPow(new a.default(this.hexHash(this.SaltToHashDevices+r),16),this.N,function(e,t){e&&n(e,null),i.verifierDevices=i.padHex(t),n(null,null)})},e.prototype.calculateA=function(e,t){var n=this;this.g.modPow(e,this.N,function(e,i){e&&t(e,null),i.mod(n.N).equals(a.default.ZERO)&&t(new Error("Illegal paramater. A mod N cannot be 0."),null),t(null,i)})},e.prototype.calculateU=function(e,t){this.UHexHash=this.hexHash(this.padHex(e)+this.padHex(t));var n=new a.default(this.UHexHash,16);return n},e.prototype.hash=function(e){var t=o.util.crypto.sha256(e,"hex");return new Array(64-t.length).join("0")+t},e.prototype.hexHash=function(e){return this.hash(new o.util.Buffer(e,"hex"))},e.prototype.computehkdf=function(e,t){var n=o.util.crypto.hmac(t,e,"buffer","sha256"),i=o.util.buffer.concat([this.infoBits,new o.util.Buffer(String.fromCharCode(1),"utf8")]),s=o.util.crypto.hmac(n,i,"buffer","sha256");return s.slice(0,16)},e.prototype.getPasswordAuthenticationKey=function(e,t,n,i,s){var r=this;if(n.mod(this.N).equals(a.default.ZERO))throw new Error("B cannot be zero.");if(this.UValue=this.calculateU(this.largeAValue,n),this.UValue.equals(a.default.ZERO))throw new Error("U cannot be zero.");var u=""+this.poolName+e+":"+t,c=this.hash(u),l=new a.default(this.hexHash(this.padHex(i)+c),16);this.calculateS(l,n,function(e,t){e&&s(e,null);var n=r.computehkdf(new o.util.Buffer(r.padHex(t),"hex"),new o.util.Buffer(r.padHex(r.UValue.toString(16)),"hex"));s(null,n)})},e.prototype.calculateS=function(e,t,n){var i=this;this.g.modPow(e,this.N,function(s,o){s&&n(s,null);var r=t.subtract(i.k.multiply(o));r.modPow(i.smallAValue.add(i.UValue.multiply(e)),i.N,function(e,t){e&&n(e,null),n(null,t.mod(i.N))})})},e.prototype.getNewPasswordRequiredChallengeUserAttributePrefix=function(){return c},e.prototype.padHex=function(e){var t=e.toString(16);return t.length%2===1?t="0"+t:"89ABCDEFabcdef".indexOf(t[0])!==-1&&(t="00"+t),t},e}();t.default=l},function(e,t){"use strict";function n(e,t){null!=e&&this.fromString(e,t)}function i(){return new n(null)}function s(e,t,n,i,s,o){for(;--o>=0;){var r=t*this[e++]+n[i]+s;s=Math.floor(r/67108864),n[i++]=67108863&r}return s}function o(e,t,n,i,s,o){for(var r=32767&t,a=t>>15;--o>=0;){var u=32767&this[e],c=this[e++]>>15,l=a*u+c*r;u=r*u+((32767&l)<<15)+n[i]+(1073741823&s),s=(u>>>30)+(l>>>15)+a*c+(s>>>30),n[i++]=1073741823&u}return s}function r(e,t,n,i,s,o){for(var r=16383&t,a=t>>14;--o>=0;){var u=16383&this[e],c=this[e++]>>14,l=a*u+c*r;u=r*u+((16383&l)<<14)+n[i]+s,s=(u>>28)+(l>>14)+a*c,n[i++]=268435455&u}return s}function a(e){return z.charAt(e)}function u(e,t){var n=Q[e.charCodeAt(t)];return null==n?-1:n}function c(e){for(var t=this.t-1;t>=0;--t)e[t]=this[t];e.t=this.t,e.s=this.s}function l(e){this.t=1,this.s=e<0?-1:0,e>0?this[0]=e:e<-1?this[0]=e+this.DV:this.t=0}function h(e){var t=i();return t.fromInt(e),t}function f(e,t){var i;if(16==t)i=4;else if(8==t)i=3;else if(2==t)i=1;else if(32==t)i=5;else{if(4!=t)throw new Error("Only radix 2, 4, 8, 16, 32 are supported");i=2}this.t=0,this.s=0;for(var s=e.length,o=!1,r=0;--s>=0;){var a=u(e,s);a<0?"-"==e.charAt(s)&&(o=!0):(o=!1,0==r?this[this.t++]=a:r+i>this.DB?(this[this.t-1]|=(a&(1<>this.DB-r):this[this.t-1]|=a<=this.DB&&(r-=this.DB))}this.clamp(),o&&n.ZERO.subTo(this,this)}function d(){for(var e=this.s&this.DM;this.t>0&&this[this.t-1]==e;)--this.t}function p(e){if(this.s<0)return"-"+this.negate().toString();var t;if(16==e)t=4;else if(8==e)t=3;else if(2==e)t=1;else if(32==e)t=5;else{if(4!=e)throw new Error("Only radix 2, 4, 8, 16, 32 are supported");t=2}var n,i=(1<0)for(u>u)>0&&(s=!0,o=a(n));r>=0;)u>(u+=this.DB-t)):(n=this[r]>>(u-=t)&i,u<=0&&(u+=this.DB,--r)),n>0&&(s=!0),s&&(o+=a(n));return s?o:"0"}function g(){var e=i();return n.ZERO.subTo(this,e),e}function v(){return this.s<0?this.negate():this}function S(e){var t=this.s-e.s;if(0!=t)return t;var n=this.t;if(t=n-e.t,0!=t)return this.s<0?-t:t;for(;--n>=0;)if(0!=(t=this[n]-e[n]))return t;return 0}function m(e){var t,n=1;return 0!=(t=e>>>16)&&(e=t,n+=16),0!=(t=e>>8)&&(e=t,n+=8),0!=(t=e>>4)&&(e=t,n+=4),0!=(t=e>>2)&&(e=t,n+=2),0!=(t=e>>1)&&(e=t,n+=1),n}function C(){return this.t<=0?0:this.DB*(this.t-1)+m(this[this.t-1]^this.s&this.DM)}function y(e,t){var n;for(n=this.t-1;n>=0;--n)t[n+e]=this[n];for(n=e-1;n>=0;--n)t[n]=0;t.t=this.t+e,t.s=this.s}function U(e,t){for(var n=e;n=0;--n)t[n+r+1]=this[n]>>s|a,a=(this[n]&o)<=0;--n)t[n]=0;t[r]=a,t.t=this.t+r+1,t.s=this.s,t.clamp()}function w(e,t){t.s=this.s;var n=Math.floor(e/this.DB);if(n>=this.t)return void(t.t=0);var i=e%this.DB,s=this.DB-i,o=(1<>i;for(var r=n+1;r>i;i>0&&(t[this.t-n-1]|=(this.s&o)<>=this.DB;if(e.t>=this.DB;i+=this.s}else{for(i+=this.s;n>=this.DB;i-=e.s}t.s=i<0?-1:0,i<-1?t[n++]=this.DV+i:i>0&&(t[n++]=i),t.t=n,t.clamp()}function D(e,t){var i=this.abs(),s=e.abs(),o=i.t;for(t.t=o+s.t;--o>=0;)t[o]=0;for(o=0;o=0;)e[n]=0;for(n=0;n=t.DV&&(e[n+t.t]-=t.DV,e[n+t.t+1]=1)}e.t>0&&(e[e.t-1]+=t.am(n,t[n],e,2*n,0,1)),e.s=0,e.clamp()}function E(e,t,s){var o=e.abs();if(!(o.t<=0)){var r=this.abs();if(r.t0?(o.lShiftTo(l,a),r.lShiftTo(l,s)):(o.copyTo(a),r.copyTo(s));var h=a.t,f=a[h-1];if(0!=f){var d=f*(1<1?a[h-2]>>this.F2:0),p=this.FV/d,g=(1<=0&&(s[s.t++]=1,s.subTo(y,s)),n.ONE.dlShiftTo(h,y),y.subTo(a,a);a.t=0;){var U=s[--S]==f?this.DM:Math.floor(s[S]*p+(s[S-1]+v)*g);if((s[S]+=a.am(0,U,s,C,0,h))0&&s.rShiftTo(l,s),u<0&&n.ZERO.subTo(s,s)}}}function k(e){var t=i();return this.abs().divRemTo(e,null,t),this.s<0&&t.compareTo(n.ZERO)>0&&e.subTo(t,t),t}function R(){if(this.t<1)return 0;var e=this[0];if(0==(1&e))return 0;var t=3&e;return t=t*(2-(15&e)*t)&15,t=t*(2-(255&e)*t)&255,t=t*(2-((65535&e)*t&65535))&65535,t=t*(2-e*t%this.DV)%this.DV,t>0?this.DV-t:-t}function F(e){return 0==this.compareTo(e)}function P(e,t){for(var n=0,i=0,s=Math.min(e.t,this.t);n>=this.DB;if(e.t>=this.DB;i+=this.s}else{for(i+=this.s;n>=this.DB;i+=e.s}t.s=i<0?-1:0,i>0?t[n++]=i:i<-1&&(t[n++]=this.DV+i),t.t=n,t.clamp()}function b(e){var t=i();return this.addTo(e,t),t}function _(e){var t=i();return this.subTo(e,t),t}function M(e){var t=i();return this.multiplyTo(e,t),t}function x(e){var t=i();return this.divRemTo(e,t,null),t}function N(e){this.m=e,this.mp=e.invDigit(),this.mpl=32767&this.mp,this.mph=this.mp>>15,this.um=(1<0&&this.m.subTo(t,t),t}function O(e){var t=i();return e.copyTo(t),this.reduce(t),t}function V(e){for(;e.t<=this.mt2;)e[e.t++]=0;for(var t=0;t>15)*this.mpl&this.um)<<15)&e.DM;for(n=t+this.m.t,e[n]+=this.m.am(0,i,e,t,0,this.m.t);e[n]>=e.DV;)e[n]-=e.DV,e[++n]++}e.clamp(),e.drShiftTo(this.m.t,e),e.compareTo(this.m)>=0&&e.subTo(this.m,e)}function K(e,t){e.squareTo(t),this.reduce(t)}function q(e,t,n){e.multiplyTo(t,n),this.reduce(n)}function j(e,t,n){var s,o=e.bitLength(),r=h(1),a=new N(t);if(o<=0)return r;s=o<18?1:o<48?3:o<144?4:o<768?5:6;var u=new Array,c=3,l=s-1,f=(1<1){var d=i();for(a.sqrTo(u[1],d);c<=f;)u[c]=i(),a.mulTo(d,u[c-2],u[c]),c+=2}var p,g,v=e.t-1,S=!0,C=i();for(o=m(e[v])-1;v>=0;){for(o>=l?p=e[v]>>o-l&f:(p=(e[v]&(1<0&&(p|=e[v-1]>>this.DB+o-l)),c=s;0==(1&p);)p>>=1,--c;if((o-=c)<0&&(o+=this.DB,--v),S)u[p].copyTo(r),S=!1;else{for(;c>1;)a.sqrTo(r,C),a.sqrTo(C,r),c-=2;c>0?a.sqrTo(r,C):(g=r,r=C,C=g),a.mulTo(C,u[p],r)}for(;v>=0&&0==(e[v]&1<0&&void 0!==arguments[0]?arguments[0]:{},i=n.AccessToken;return s(this,t),o(this,e.call(this,i||""))}return r(t,e),t}(u.default);t.default=c},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function r(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}t.__esModule=!0;var a=n(6),u=i(a),c=function(e){function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},i=n.IdToken;return s(this,t),o(this,e.call(this,i||""))}return r(t,e),t}(u.default);t.default=c},function(e,t,n){"use strict";function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}t.__esModule=!0;var s=n(1),o=function(){function e(t){i(this,e),this.jwtToken=t||"",this.payload=this.decodePayload()}return e.prototype.getJwtToken=function(){return this.jwtToken},e.prototype.getExpiration=function(){return this.payload.exp},e.prototype.getIssuedAt=function(){return this.payload.iat},e.prototype.decodePayload=function(){var e=this.jwtToken.split(".")[1];try{return JSON.parse(s.util.base64.decode(e).toString("utf8"))}catch(e){return{}}},e}();t.default=o},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}t.__esModule=!0;/*! 18 | * Copyright 2016 Amazon.com, 19 | * Inc. or its affiliates. All Rights Reserved. 20 | * 21 | * Licensed under the Amazon Software License (the "License"). 22 | * You may not use this file except in compliance with the 23 | * License. A copy of the License is located at 24 | * 25 | * http://aws.amazon.com/asl/ 26 | * 27 | * or in the "license" file accompanying this file. This file is 28 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 29 | * CONDITIONS OF ANY KIND, express or implied. See the License 30 | * for the specific language governing permissions and 31 | * limitations under the License. 32 | */ 33 | var i=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},i=t.RefreshToken;n(this,e),this.token=i||""}return e.prototype.getToken=function(){return this.token},e}();t.default=i},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}t.__esModule=!0;var o=n(1),r=n(3),a=i(r),u=n(2),c=i(u),l=n(4),h=i(l),f=n(5),d=i(f),p=n(7),g=i(p),v=n(10),S=i(v),m=n(11),C=i(m),y=n(9),U=i(y),A=n(12),w=i(A),T=function(){function e(t){if(s(this,e),null==t||null==t.Username||null==t.Pool)throw new Error("Username and pool information are required.");this.username=t.Username||"",this.pool=t.Pool,this.Session=null,this.client=t.Pool.client,this.signInUserSession=null,this.authenticationFlowType="USER_SRP_AUTH",this.storage=t.Storage||(new w.default).getStorage()}return e.prototype.setSignInUserSession=function(e){this.clearCachedTokens(),this.signInUserSession=e,this.cacheTokens()},e.prototype.getSignInUserSession=function(){return this.signInUserSession},e.prototype.getUsername=function(){return this.username},e.prototype.getAuthenticationFlowType=function(){return this.authenticationFlowType},e.prototype.setAuthenticationFlowType=function(e){this.authenticationFlowType=e},e.prototype.initiateAuth=function(e,t){var n=this,i=e.getAuthParameters();i.USERNAME=this.username;var s={AuthFlow:"CUSTOM_AUTH",ClientId:this.pool.getClientId(),AuthParameters:i,ClientMetadata:e.getValidationData()};this.getUserContextData()&&(s.UserContextData=this.getUserContextData()),this.client.makeUnauthenticatedRequest("initiateAuth",s,function(e,i){if(e)return t.onFailure(e);var s=i.ChallengeName,o=i.ChallengeParameters;return"CUSTOM_CHALLENGE"===s?(n.Session=i.Session,t.customChallenge(o)):(n.signInUserSession=n.getCognitoUserSession(i.AuthenticationResult),n.cacheTokens(),t.onSuccess(n.signInUserSession))})},e.prototype.authenticateUser=function(e,t){var n=this,i=new c.default(this.pool.getUserPoolId().split("_")[1]),s=new C.default,r=void 0,u=void 0,l={};null!=this.deviceKey&&(l.DEVICE_KEY=this.deviceKey),l.USERNAME=this.username,i.getLargeAValue(function(c,h){c&&t.onFailure(c),l.SRP_A=h.toString(16),"CUSTOM_AUTH"===n.authenticationFlowType&&(l.CHALLENGE_NAME="SRP_A");var f={AuthFlow:n.authenticationFlowType,ClientId:n.pool.getClientId(),AuthParameters:l,ClientMetadata:e.getValidationData()};n.getUserContextData(n.username)&&(f.UserContextData=n.getUserContextData(n.username)),n.client.makeUnauthenticatedRequest("initiateAuth",f,function(c,l){if(c)return t.onFailure(c);var h=l.ChallengeParameters;n.username=h.USER_ID_FOR_SRP,r=new a.default(h.SRP_B,16),u=new a.default(h.SALT,16),n.getCachedDeviceKeyAndPassword(),i.getPasswordAuthenticationKey(n.username,e.getPassword(),r,u,function(e,r){e&&t.onFailure(e);var a=s.getNowString(),u=o.util.crypto.hmac(r,o.util.buffer.concat([new o.util.Buffer(n.pool.getUserPoolId().split("_")[1],"utf8"),new o.util.Buffer(n.username,"utf8"),new o.util.Buffer(h.SECRET_BLOCK,"base64"),new o.util.Buffer(a,"utf8")]),"base64","sha256"),c={};c.USERNAME=n.username,c.PASSWORD_CLAIM_SECRET_BLOCK=h.SECRET_BLOCK,c.TIMESTAMP=a,c.PASSWORD_CLAIM_SIGNATURE=u,null!=n.deviceKey&&(c.DEVICE_KEY=n.deviceKey);var f=function e(t,i){return n.client.makeUnauthenticatedRequest("respondToAuthChallenge",t,function(s,o){return s&&"ResourceNotFoundException"===s.code&&s.message.toLowerCase().indexOf("device")!==-1?(c.DEVICE_KEY=null,n.deviceKey=null,n.randomPassword=null,n.deviceGroupKey=null,n.clearCachedDeviceKeyAndPassword(),e(t,i)):i(s,o)})},d={ChallengeName:"PASSWORD_VERIFIER",ClientId:n.pool.getClientId(),ChallengeResponses:c,Session:l.Session};n.getUserContextData()&&(d.UserContextData=n.getUserContextData()),f(d,function(e,s){if(e)return t.onFailure(e);var o=s.ChallengeName;if("NEW_PASSWORD_REQUIRED"===o){n.Session=s.Session;var r=null,a=null,u=[],c=i.getNewPasswordRequiredChallengeUserAttributePrefix();if(s.ChallengeParameters&&(r=JSON.parse(s.ChallengeParameters.userAttributes),a=JSON.parse(s.ChallengeParameters.requiredAttributes)),a)for(var l=0;l0&&void 0!==arguments[0]?arguments[0]:{},i=t.Name,s=t.Value;n(this,e),this.Name=i||"",this.Value=s||""}return e.prototype.getValue=function(){return this.Value},e.prototype.setValue=function(e){return this.Value=e,this},e.prototype.getName=function(){return this.Name},e.prototype.setName=function(e){return this.Name=e,this},e.prototype.toString=function(){return JSON.stringify(this)},e.prototype.toJSON=function(){return{Name:this.Name,Value:this.Value}},e}();t.default=i},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}t.__esModule=!0;/*! 50 | * Copyright 2016 Amazon.com, 51 | * Inc. or its affiliates. All Rights Reserved. 52 | * 53 | * Licensed under the Amazon Software License (the "License"). 54 | * You may not use this file except in compliance with the 55 | * License. A copy of the License is located at 56 | * 57 | * http://aws.amazon.com/asl/ 58 | * 59 | * or in the "license" file accompanying this file. This file is 60 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 61 | * CONDITIONS OF ANY KIND, express or implied. See the License 62 | * for the specific language governing permissions and 63 | * limitations under the License. 64 | */ 65 | var i=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},i=t.IdToken,s=t.RefreshToken,o=t.AccessToken,r=t.ClockDrift;if(n(this,e),null==o||null==i)throw new Error("Id token and Access Token must be present.");this.idToken=i,this.refreshToken=s,this.accessToken=o,this.clockDrift=void 0===r?this.calculateClockDrift():r}return e.prototype.getIdToken=function(){return this.idToken},e.prototype.getRefreshToken=function(){return this.refreshToken},e.prototype.getAccessToken=function(){return this.accessToken},e.prototype.getClockDrift=function(){return this.clockDrift},e.prototype.calculateClockDrift=function(){var e=Math.floor(new Date/1e3),t=Math.min(this.accessToken.getIssuedAt(),this.idToken.getIssuedAt());return e-t},e.prototype.isValid=function(){var e=Math.floor(new Date/1e3),t=e-this.clockDrift;return t1){if(o=e({path:"/"},i.defaults,o),"number"==typeof o.expires){var a=new Date;a.setMilliseconds(a.getMilliseconds()+864e5*o.expires),o.expires=a}o.expires=o.expires?o.expires.toUTCString():"";try{r=JSON.stringify(s),/^[\{\[]/.test(r)&&(s=r)}catch(e){}s=n.write?n.write(s,t):encodeURIComponent(String(s)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),t=encodeURIComponent(String(t)),t=t.replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent),t=t.replace(/[\(\)]/g,escape);var u="";for(var c in o)o[c]&&(u+="; "+c,o[c]!==!0&&(u+="="+o[c]));return document.cookie=t+"="+s+u}t||(r={});for(var l=document.cookie?document.cookie.split("; "):[],h=/(%[0-9A-Z]{2})+/g,f=0;f 2 | 3 | 4 | 8 | 9 | 10 |
11 |
12 |
13 |
14 |
15 |
16 |
Username
17 | 19 |
20 |
21 |
Code
22 | 24 |
25 | 30 |
31 |
32 |
33 |
34 |
35 | 38 | 40 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 |
11 |
12 |
13 |

some content

14 |
15 |
16 |
17 |
18 |
19 |
Username
20 | 23 |
24 |
25 |
Password
26 | 29 |
30 | 34 |
35 |
36 |
37 |
38 |
39 |
Username
40 | 43 |
44 |
45 |
Email address
46 | 49 |
50 |
51 |
Password
52 | 55 |
56 | 60 |
61 |
62 |
63 |
64 |
65 | 68 | 70 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /templates/welcome.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 |
Hello
11 |
12 |
13 |
14 | 17 | 18 | 21 | 23 | 25 | 26 | 29 | 30 | 31 | --------------------------------------------------------------------------------