2020-02-05 17:59:54 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 Balena
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } from '../utils/lazy';
|
2020-02-05 17:59:54 +00:00
|
|
|
|
|
|
|
/**
|
2022-01-19 21:42:54 +00:00
|
|
|
* Get dashboard CLI login URL
|
2020-02-05 17:59:54 +00:00
|
|
|
*
|
2022-01-19 21:42:54 +00:00
|
|
|
* @param callbackUrl - Callback url, e.g. 'http://127.0.0.1:3000'
|
|
|
|
* @returns Dashboard login URL, e.g.:
|
|
|
|
* 'https://dashboard.balena-cloud.com/login/cli/http%253A%252F%252F127.0.0.1%253A59581%252Fauth'
|
2020-02-05 17:59:54 +00:00
|
|
|
*/
|
2022-01-19 21:42:54 +00:00
|
|
|
export async function getDashboardLoginURL(
|
|
|
|
callbackUrl: string,
|
|
|
|
): Promise<string> {
|
2020-02-05 17:59:54 +00:00
|
|
|
// Encode percentages signs from the escaped url
|
|
|
|
// characters to avoid angular getting confused.
|
|
|
|
callbackUrl = encodeURIComponent(callbackUrl).replace(/%/g, '%25');
|
|
|
|
|
2022-01-19 21:42:54 +00:00
|
|
|
const [{ URL }, dashboardUrl] = await Promise.all([
|
|
|
|
import('url'),
|
|
|
|
getBalenaSdk().settings.get('dashboardUrl'),
|
|
|
|
]);
|
|
|
|
return new URL(`/login/cli/${callbackUrl}`, dashboardUrl).href;
|
|
|
|
}
|
2020-02-05 17:59:54 +00:00
|
|
|
|
|
|
|
/**
|
2022-01-19 21:42:54 +00:00
|
|
|
* Log in using a token, but only if the token is valid.
|
2020-02-05 17:59:54 +00:00
|
|
|
*
|
|
|
|
* This function checks that the token is not only well-structured
|
|
|
|
* but that it also authenticates with the server successfully.
|
|
|
|
*
|
|
|
|
* If authenticated, the token is persisted, if not then the previous
|
|
|
|
* login state is restored.
|
|
|
|
*
|
2022-01-19 21:42:54 +00:00
|
|
|
* @param token - session token or api key
|
|
|
|
* @returns whether the login was successful or not
|
2020-02-05 17:59:54 +00:00
|
|
|
*/
|
2022-01-19 21:42:54 +00:00
|
|
|
export async function loginIfTokenValid(token?: string): Promise<boolean> {
|
|
|
|
token = (token || '').trim();
|
|
|
|
if (!token) {
|
2020-07-29 17:27:20 +00:00
|
|
|
return false;
|
2020-02-05 17:59:54 +00:00
|
|
|
}
|
2020-02-27 14:55:30 +00:00
|
|
|
const balena = getBalenaSdk();
|
2020-02-05 17:59:54 +00:00
|
|
|
|
2020-07-31 15:25:48 +00:00
|
|
|
let currentToken;
|
|
|
|
try {
|
|
|
|
currentToken = await balena.auth.getToken();
|
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
2020-02-05 17:59:54 +00:00
|
|
|
|
2020-07-31 15:25:48 +00:00
|
|
|
await balena.auth.loginWithToken(token);
|
|
|
|
const isLoggedIn = await balena.auth.isLoggedIn();
|
|
|
|
if (!isLoggedIn) {
|
|
|
|
if (currentToken != null) {
|
|
|
|
await balena.auth.loginWithToken(currentToken);
|
|
|
|
} else {
|
|
|
|
await balena.auth.logout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isLoggedIn;
|
2022-01-19 21:42:54 +00:00
|
|
|
}
|