Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. import React from 'react'
  2. import Nav from "./Nav";
  3. import {
  4. BrowserRouter as Router,
  5. Route,
  6. Redirect
  7. } from 'react-router-dom'
  8.  
  9. const Config = {
  10. 'authorizationEndpoint': 'https://demo-api.gii.cloud/api/oauth/auth',
  11. 'tokenEndpoint': 'https://demo-api.gii.cloud/api/oauth/token',
  12. 'revocationEndpoint': 'https://demo-api.gii.cloud/api/oauth/revoke',
  13. 'clientId': 'e-avrop',
  14. 'response_type': 'code',
  15. 'redirectUri': 'http://localhost:8080/api/authentication/v1',
  16. 'scope': 'openid',
  17. 'state': 'foobar-6129484611666145821',
  18. 'display': 'page',
  19. 'identityprovider':'google'
  20. };
  21.  
  22. const OidcValues = {
  23. 'accessToken': 'unknown',
  24. 'idToken': 'unknown'
  25. };
  26.  
  27. const AuthStatus = {
  28. isAuthenticated: false
  29. };
  30.  
  31. const UrlHelper = {
  32. getAuthUrl(authorizationEndpoint, originalUrl, nonce) {
  33. var encodedOriginal = encodeURI(originalUrl);
  34. var redirectUri = encodeURI(Config.redirectUri);
  35.  
  36. return Config.authorizationEndpoint +
  37. '?response_type='+ Config.response_type +
  38. '&client_id=' + Config.clientId +
  39. '&scope=' + Config.scope +
  40. '&redirect_uri=' + redirectUri +
  41. '&display=' + Config.display +
  42. '&nonce=' + nonce +
  43. '&state=' + Config.state +
  44. '&identity_provider=' + Config.identityprovider
  45. },
  46.  
  47. generateNonce(length) {
  48. var text = "";
  49. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  50.  
  51. for(var i = 0; i < length; i++) {
  52. text += possible.charAt(Math.floor(Math.random() * possible.length));
  53. }
  54.  
  55. return text;
  56. },
  57.  
  58. getParams(url) {
  59. var regex = /[#?&]([^=#]+)=([^&#]*)/g,
  60. params = {},
  61. match;
  62.  
  63. // eslint-disable-next-line
  64. while (match = regex.exec(url)) {
  65. params[match[1]] = match[2];
  66. }
  67.  
  68. return params;
  69. }
  70. };
  71.  
  72. const StorageHelper = {
  73. saveNonce(nonce) {
  74. if (typeof(Storage) !== "undefined") {
  75. localStorage.setItem("nonce", nonce);
  76. }
  77. },
  78.  
  79. loadNonce() {
  80. if (typeof(Storage) !== "undefined") {
  81. var nonce = localStorage.getItem("nonce");
  82.  
  83. localStorage.removeItem("nonce");
  84.  
  85. return nonce;
  86. }
  87. }
  88. };
  89.  
  90. const ReactOidcExample = () => (
  91. <Router>
  92. <div className="container">
  93. <Nav/>
  94.  
  95. <Route path="/" exact component={Home}/>
  96. <Route path="/login" component={Login}/>
  97. <Route path="/authentication/callback" component={AuthenticationCallback}/>
  98.  
  99. <PrivateRoute path="/tokens/access" component={AccessToken}/>
  100. <PrivateRoute path="/tokens/id" component={IdToken}/>
  101. </div>
  102. </Router>
  103. )
  104.  
  105. const PrivateRoute = ({ component: Component, ...rest }) => (
  106. <Route {...rest} render={props => (
  107. AuthStatus.isAuthenticated ? (
  108. <Component {...props}/>
  109. ) : (
  110. <Redirect to={{
  111. pathname: '/login',
  112. state: { from: props.location }
  113. }}/>
  114. )
  115. )}/>
  116. )
  117.  
  118. const Home = () => (
  119. <div className="mt">
  120. <h1>React GII Sample</h1>
  121.  
  122. <p>
  123. Welcome!
  124. </p>
  125. </div>
  126. );
  127.  
  128. const AccessToken = () => (
  129. <div className="mt">
  130. <div className="panel panel-default">
  131. <div className="panel-heading">
  132. <h3 className="panel-title">Access Token</h3>
  133. </div>
  134. <div className="panel-body">
  135. {OidcValues.accessToken}
  136. </div>
  137. </div>
  138. </div>
  139. );
  140.  
  141. const IdToken = () => (
  142. <div className="mt">
  143. <div className="panel panel-default">
  144. <div className="panel-heading">
  145. <h3 className="panel-title">ID Token</h3></div>
  146. <div className="panel-body">
  147. {OidcValues.idToken}
  148. </div>
  149. </div>
  150. </div>
  151. );
  152.  
  153. class AuthenticationCallback extends React.Component {
  154. constructor(props) {
  155. super(props);
  156. this.redirectTo = '/';
  157. }
  158.  
  159. componentWillMount() {
  160. // eslint-disable-next-line
  161. var storedNonce = StorageHelper.loadNonce();
  162. var params = UrlHelper.getParams(window.location.href);
  163.  
  164. OidcValues.idToken = params['id_token'];
  165. OidcValues.accessToken = params['access_token'];
  166.  
  167. this.redirectTo = decodeURIComponent(params['state']);
  168.  
  169. // Set authenticated to true
  170. AuthStatus.isAuthenticated = true;
  171. }
  172.  
  173. render() {
  174. return (
  175. <Redirect to={this.redirectTo} />
  176. )
  177. }
  178. }
  179.  
  180. class Login extends React.Component {
  181. state = {
  182. redirectToReferrer: false
  183. }
  184.  
  185. componentWillMount() {
  186. const { from } = this.props.location.state || { from: { pathname: '/' } };
  187.  
  188. var nonce = UrlHelper.generateNonce(32);
  189. StorageHelper.saveNonce(nonce);
  190.  
  191. var authUrl = UrlHelper.getAuthUrl(Config.authorizationEndpoint, from.pathname, nonce);
  192.  
  193. console.log('Redirecting: ' + authUrl);
  194.  
  195. window.location = authUrl;
  196. }
  197.  
  198. render() {
  199. return (
  200. <section>Redirecting to SSO...</section>
  201. )
  202. }
  203. }
  204.  
  205. export default ReactOidcExample;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement