Commit 4a157ac8 authored by Ben Galloway's avatar Ben Galloway

Initial commit

parents
{
"presets": [
"@babel/preset-react",
[
"@babel/preset-env",
{
"useBuiltIns": "entry",
"targets": ">0.2%, not dead, not ie < 11, not op_mini all"
}
]
]
}
# https://github.com/github/gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isTokenExpired = exports.GSCAuthConsumer = exports.GSCAuthProvider = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactAadMsal = require("react-aad-msal");
var _jwtDecode = _interopRequireDefault(require("jwt-decode"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
var isTokenExpired = function isTokenExpired(token) {
try {
var decoded = (0, _jwtDecode.default)(token);
if (decoded.exp < Date.now() / 1000) {
return true;
} else return false;
} catch (err) {
return false;
}
};
exports.isTokenExpired = isTokenExpired;
var emptyAuthObject = {
userInfo: {
jwtAccessToken: "",
jwtIdToken: "",
user: {}
},
login: function login() {},
logout: function logout() {}
};
var GSCAuthContext = _react.default.createContext(emptyAuthObject);
var GSCAuthConsumer = GSCAuthContext.Consumer;
exports.GSCAuthConsumer = GSCAuthConsumer;
var GSCAuthProvider =
/*#__PURE__*/
function (_React$Component) {
_inherits(GSCAuthProvider, _React$Component);
function GSCAuthProvider() {
var _this;
_classCallCheck(this, GSCAuthProvider);
_this = _possibleConstructorReturn(this, _getPrototypeOf(GSCAuthProvider).call(this, props));
_this.state = emptyAuthObject;
_this.loginCallback = _this.loginCallback.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.logoutCallback = _this.logoutCallback.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.userInfoCallback = _this.userInfoCallback.bind(_assertThisInitialized(_assertThisInitialized(_this)));
return _this;
}
_createClass(GSCAuthProvider, [{
key: "loginCallback",
value: function loginCallback(login) {
this.setState({
login: login
});
}
}, {
key: "logoutCallback",
value: function logoutCallback(logout) {
this.setState({
logout: logout
});
}
}, {
key: "userInfoCallback",
value: function userInfoCallback(userInfo) {
this.setState({
userInfo: userInfo
});
}
}, {
key: "render",
value: function render() {
return _react.default.createElement(_reactAadMsal.AzureAD, {
provider: new _reactAadMsal.MsalAuthProviderFactory({
clientID: this.props.appId,
scopes: this.props.scopes || [],
type: _reactAadMsal.LoginType[this.props.loginType || "Redirect"],
persistLoginPastSession: this.props.persistLoginPastSession || false
}),
unauthenticatedFunction: this.loginCallback,
authenticatedFunction: this.logoutCallback,
userInfoCallback: this.userInfoCallback
}, _react.default.createElement(GSCAuthContext.Provider, {
value: this.state
}, this.props.children));
}
}]);
return GSCAuthProvider;
}(_react.default.Component);
exports.GSCAuthProvider = GSCAuthProvider;
\ No newline at end of file
{
"name": "@gsc/react-auth",
"version": "1.0.0",
"description": "React components to obtain a JWT from MS AAD and make it available on Context for use in API calls",
"main": "dist/index.js",
"author": "Ben Galloway <ben.galloway@gsc.org.uk>",
"license": "MIT",
"private": true,
"scripts": {
"build": "NODE_ENV=production babel src -d dist --copy-files"
},
"dependencies": {
"@babel/polyfill": "^7.2.5",
"jwt-decode": "^2.2.0",
"react-aad-msal": "^0.4.1"
},
"peerDependencies": {
"react": "^16.7.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0"
}
}
import React from "react";
import { AzureAD, MsalAuthProviderFactory, LoginType } from "react-aad-msal";
import decode from "jwt-decode";
const isTokenExpired = token => {
try {
const decoded = decode(token);
if (decoded.exp < Date.now() / 1000) {
return true;
} else return false;
} catch (err) {
return false;
}
};
const emptyAuthObject = {
userInfo: {
jwtAccessToken: "",
jwtIdToken: "",
user: {},
},
login: () => {},
logout: () => {},
};
const GSCAuthContext = React.createContext(emptyAuthObject);
const GSCAuthConsumer = GSCAuthContext.Consumer;
class GSCAuthProvider extends React.Component {
constructor() {
super(props);
this.state = emptyAuthObject;
this.loginCallback = this.loginCallback.bind(this);
this.logoutCallback = this.logoutCallback.bind(this);
this.userInfoCallback = this.userInfoCallback.bind(this);
}
loginCallback(login) {
this.setState({ login });
}
logoutCallback(logout) {
this.setState({ logout });
}
userInfoCallback(userInfo) {
this.setState({ userInfo });
}
render() {
return (
<AzureAD
provider={
new MsalAuthProviderFactory({
clientID: this.props.appId,
scopes: this.props.scopes || [],
type: LoginType[this.props.loginType || "Redirect"],
persistLoginPastSession: this.props.persistLoginPastSession || false,
})
}
unauthenticatedFunction={this.loginCallback}
authenticatedFunction={this.logoutCallback}
userInfoCallback={this.userInfoCallback}
>
<GSCAuthContext.Provider value={this.state}>{this.props.children}</GSCAuthContext.Provider>
</AzureAD>
);
}
}
export { GSCAuthProvider, GSCAuthConsumer, isTokenExpired };
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment