Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 2x 37x 37x 4x 4x 1x 3x 3x 1x 36x 13x 62x 28x 62x 2x 1x 2x 2x 2x 17x 1x 16x 2x 16x 16x 16x 2x 2x 14x 1x 1x 13x 2x 2x 11x 11x 11x 11x 1x 1x 10x 10x 1x 1x 9x 2x 2x 7x 7x 7x 1x 1x 6x 3x 3x 3x 3x 3x 3x 16x 3x 13x | /**
* @prettier
*/
/*
* Part of Pleiar.no - a collection of tools for nurses
*
* Copyright (C) Fagforbundet 2019
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// @flow
// Let flow know about our AUTH_URL, AUTH_ALLOW_URL and AUTH_DEFAULT_STATE
// globals (from webpack/data config)
declare var AUTH_URL: string;
declare var AUTH_ALLOW_URL: RegExp;
declare var AUTH_DEFAULT_STATE: boolean;
export type AuthError =
| "JSON"
| "PAYLOAD"
| "CONTENTID"
| "CONTENTIAT"
| "TIME"
| "NOREF"
| "REF";
type AuthObjType = {|
initialized: boolean,
authenticated: boolean,
_error: AuthError | null,
initialize: () => void,
isAuthenticated: () => boolean,
invalidate: () => void,
authenticate: (string) => boolean,
getError: () => AuthError | null,
|};
/**
* A basic authentication helper. It stores authentication state between sessions,
* and performs validation of authentication (JWT) tokens. It can then be queried at
* any point for the actual authentication state (by calling isAuthenticated).
*
* TODO: Verify JWT
* TODO: Store authentication state
* TODO: Whitelist bots
*/
const auth: AuthObjType = {
/*
* AUTH_DEFAULT_STATE is taken from the data config. This defines the
* default state of any user (ie. authenticated or not). Essentially, it
* enables or disables the authentication module. It will be `false` if the
* default state is that someone is logged out, ie. authentication enabled.
* It will be `true` if the default state is that someone is logged in, ie.
* authentication disabled (everything available to everyone).
*/
initialized: AUTH_DEFAULT_STATE,
authenticated: AUTH_DEFAULT_STATE,
_error: null,
/**
* This method initializes an auth object. It will load auth state (if any).
*/
initialize() {
auth.initialized = true;
if (window.localStorage.pau !== undefined && window.localStorage.pau) {
// FIXME: typing
let state: {||};
try {
state = JSON.parse(atob(window.localStorage.pau));
} catch (e) {
return;
}
// Current time in unix time
const now = Math.round(new Date().getTime() / 1000);
if (state.date < now + 1 && state.date > now - 31536000) {
auth.authenticated = true;
}
}
// Authentication whitelists for bots and react-snap
if (
/(googlebot|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot|developers\.google\.com|ReactSnap)/i.test(
navigator.userAgent,
)
) {
auth.authenticated = true;
}
},
/**
* Checks if the current user is authenticated or not. Returns `true` if the user is
* authenticated, `false` otherwise.
*/
isAuthenticated(): boolean {
if (auth.initialized === false) {
auth.initialize();
}
return auth.authenticated;
},
/**
* Invalidates the current login, regardless of current state
*/
invalidate() {
if (auth.initialized === false) {
auth.initialize();
}
auth.authenticated = false;
delete window.localStorage.pau;
delete window.localStorage.pleiarAuthRedirect;
},
/**
* Tries to authenticate a user with the given token. If the token validates, the user
* is treated as logged in, otherwise the user is treated as logged out. Returns `true`
* if the user was logged in, `false` otherwise.
*/
authenticate(token: string): boolean {
if (AUTH_DEFAULT_STATE === true) {
return true;
}
if (auth.initialized === false) {
auth.initialize();
}
auth.authenticated = false;
/* JWT can be verified by: JWS.verifyJWT(token,{ b64: '...' }, { alg: [ 'HS256' ], }); */
let payloadObj: {||};
try {
payloadObj = JSON.parse(atob(token.split(".")[1]));
} catch (e) {
auth._error = "JSON";
return false;
}
if (
payloadObj === undefined ||
payloadObj === null ||
typeof payloadObj !== "object"
) {
auth._error = "PAYLOAD";
return false;
}
// Needs a valid memberId
if (
payloadObj.memberId === null ||
payloadObj.memberId === undefined ||
payloadObj.memberId.length === 0 ||
!/^\d+$/.test(payloadObj.memberId)
) {
auth._error = "CONTENTID";
return false;
}
// Current time in unix time
const now = Math.round(new Date().getTime() / 1000);
const minTime = now - 600;
const maxTime = now + 600;
if (payloadObj.iat === null || payloadObj.iat === undefined) {
auth._error = "CONTENTIAT";
return false;
}
const payloadIat = Number.parseInt(payloadObj.iat);
if (Number.isNaN(payloadIat)) {
auth._error = "CONTENTIAT";
return false;
}
if (payloadIat > maxTime || payloadIat < minTime) {
auth._error = "TIME";
return false;
}
const UA = window.navigator.userAgent;
const SafariITP =
document.referrer === "" &&
/AppleWebKit.*Safari/.test(UA) &&
!/(AppleWebKit\/537\.36|Chrome)/.test(UA);
// Finally, validate referrer
if (document.referrer === "" && !SafariITP) {
auth._error = "NOREF";
return false;
}
if (
document.referrer.indexOf(AUTH_URL) !== 0 &&
!AUTH_ALLOW_URL.test(document.referrer) &&
!SafariITP
) {
auth._error = "REF";
return false;
}
auth.authenticated = true;
auth._error = null;
window.localStorage.pau = btoa(JSON.stringify({ date: now }));
return true;
},
/**
* Returns the error that caused authenticate() not to succeed, or null
* if 1) the user is authenticated, or 2) we don't have an error.
*
* The error is of the type AuthError (or null)
*/
getError(): AuthError | null {
if (auth.isAuthenticated()) {
return null;
}
return auth._error;
},
};
export default auth;
|