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

mendersoftware / gui / 1301920191

23 May 2024 07:13AM UTC coverage: 83.42% (-16.5%) from 99.964%
1301920191

Pull #4421

gitlab-ci

mzedel
fix: fixed an issue that sometimes prevented reopening paginated auditlog links

Ticket: None
Changelog: Title
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4421: MEN-7034 - device information in auditlog entries

4456 of 6367 branches covered (69.99%)

34 of 35 new or added lines in 7 files covered. (97.14%)

1668 existing lines in 162 files now uncovered.

8473 of 10157 relevant lines covered (83.42%)

140.52 hits per line

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

81.4
/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();
184✔
19

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

22
let tokenCache = '';
184✔
23

24
export const getSessionInfo = () => {
184✔
25
  let sessionInfo = { ...emptySession };
370✔
26
  try {
370✔
27
    sessionInfo = JSON.parse(window.localStorage.getItem('JWT') ?? '');
370✔
28
  } catch (error) {
29
    // most likely not logged in - nothing to do here
30
  }
31
  if (sessionInfo.expiresAt && new Date(sessionInfo.expiresAt) < new Date()) {
370✔
32
    cleanUp();
7✔
33
    return { ...emptySession };
7✔
34
  }
35
  if (!sessionInfo.token) {
363✔
36
    let jwtTokenFromCookie = cookies.get('JWT', { doNotParse: true }) ?? '';
68✔
37
    if (jwtTokenFromCookie) {
68!
UNCOV
38
      setSessionInfo({ token: jwtTokenFromCookie, undefined });
×
UNCOV
39
      sessionInfo.token = jwtTokenFromCookie;
×
UNCOV
40
      cookies.remove('JWT');
×
UNCOV
41
      cookies.remove('JWT', { path: '/' });
×
42
    }
43
  }
44
  sessionInfo.token = sessionInfo.token || undefined;
363✔
45
  tokenCache = sessionInfo.token;
363✔
46
  return sessionInfo;
363✔
47
};
48

49
export const getToken = () => (tokenCache ? tokenCache : getSessionInfo().token);
1,668✔
50

51
export const setSessionInfo = ({ token, expiresAt }) => {
184✔
52
  tokenCache = token;
4✔
53
  window.localStorage.setItem('JWT', JSON.stringify({ token, expiresAt }));
4✔
54
};
55

56
export const cleanUp = () => {
184✔
57
  tokenCache = '';
18✔
58
  cookies.remove('JWT');
18✔
59
  cookies.remove('JWT', { path: '/' });
18✔
60
  window.localStorage.removeItem('JWT');
18✔
61
  window.localStorage.removeItem('oauth');
18✔
62
};
63

64
export const maxSessionAge = 900;
184✔
65

66
export const updateMaxAge = ({ expiresAt, token }) => {
184✔
67
  const oAuthExpiration = Number(window.localStorage.getItem('oauth'));
2✔
68
  let updateWithOAuth = false;
2✔
69
  if (oAuthExpiration) {
2!
UNCOV
70
    const soon = Date.now() + maxSessionAge * TIMEOUTS.oneSecond;
×
UNCOV
71
    updateWithOAuth = oAuthExpiration <= soon;
×
UNCOV
72
    if (updateWithOAuth) {
×
UNCOV
73
      window.localStorage.removeItem('oauth');
×
74
    }
75
  }
76
  if (token && expiresAt && (!oAuthExpiration || updateWithOAuth)) {
2!
77
    const expiration = new Date();
1✔
78
    expiration.setSeconds(expiration.getSeconds() + maxSessionAge);
1✔
79
    setSessionInfo({ token, expiresAt: expiration.toISOString() });
1✔
80
  }
81
};
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