• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1113439055

19 Dec 2023 09:01PM UTC coverage: 82.752% (-17.2%) from 99.964%
1113439055

Pull #4258

gitlab-ci

mender-test-bot
chore: Types update

Signed-off-by: Mender Test Bot <mender@northern.tech>
Pull Request #4258: chore: Types update

4326 of 6319 branches covered (0.0%)

8348 of 10088 relevant lines covered (82.75%)

189.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/js/auth.js
1
// Copyright 2016 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import Cookies from 'universal-cookie';
15

16
import { TIMEOUTS } from './constants/appConstants';
17

18
const cookies = new Cookies();
183✔
19

20
const emptySession = Object.freeze({ token: '', exiresAt: undefined });
183✔
21

22
let tokenCache = '';
183✔
23

24
export const getSessionInfo = () => {
183✔
25
  let sessionInfo = { ...emptySession };
369✔
26
  try {
369✔
27
    sessionInfo = JSON.parse(window.localStorage.getItem('JWT') ?? '');
369✔
28
  } catch (error) {
29
    // most likely not logged in - nothing to do here
30
  }
31
  if (sessionInfo.expiresAt && new Date(sessionInfo.expiresAt) < new Date()) {
369✔
32
    cleanUp();
6✔
33
    return { ...emptySession };
6✔
34
  }
35
  sessionInfo.token = sessionInfo.token || cookies.get('JWT', { doNotParse: true });
363✔
36
  tokenCache = sessionInfo.token;
363✔
37
  return sessionInfo;
363✔
38
};
39

40
export const getToken = () => (tokenCache ? tokenCache : getSessionInfo().token);
1,331✔
41

42
export const setSessionInfo = ({ token, expiresAt }) => {
183✔
43
  tokenCache = token;
4✔
44
  window.localStorage.setItem('JWT', JSON.stringify({ token, expiresAt }));
4✔
45
};
46

47
export const cleanUp = () => {
183✔
48
  tokenCache = '';
22✔
49
  cookies.remove('JWT');
22✔
50
  cookies.remove('JWT', { path: '/' });
22✔
51
  window.localStorage.removeItem('JWT');
22✔
52
  window.localStorage.removeItem('oauth');
22✔
53
};
54

55
export const maxSessionAge = 900;
183✔
56

57
export const updateMaxAge = ({ expiresAt, token }) => {
183✔
58
  const oAuthExpiration = Number(window.localStorage.getItem('oauth'));
2✔
59
  let updateWithOAuth = false;
2✔
60
  if (oAuthExpiration) {
2!
61
    const soon = Date.now() + maxSessionAge * TIMEOUTS.oneSecond;
×
62
    updateWithOAuth = oAuthExpiration <= soon;
×
63
    if (updateWithOAuth) {
×
64
      window.localStorage.removeItem('oauth');
×
65
    }
66
  }
67
  if (token && expiresAt && (!oAuthExpiration || updateWithOAuth)) {
2!
68
    const expiration = new Date();
1✔
69
    expiration.setSeconds(expiration.getSeconds() + maxSessionAge);
1✔
70
    setSessionInfo({ token, expiresAt: expiration.toISOString() });
1✔
71
  }
72
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc