mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-19 05:37:51 +00:00
chore: lib/auth/utils.ts: Replace deprecated url.resolve, use async/await
Change-type: patch
This commit is contained in:
parent
c0f27a663d
commit
5464e550e7
@ -14,57 +14,44 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as _ from 'lodash';
|
|
||||||
import * as url from 'url';
|
|
||||||
import { getBalenaSdk } from '../utils/lazy';
|
import { getBalenaSdk } from '../utils/lazy';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Get dashboard CLI login URL
|
* Get dashboard CLI login URL
|
||||||
* @function
|
|
||||||
* @protected
|
|
||||||
*
|
*
|
||||||
* @param {String} callbackUrl - callback url
|
* @param callbackUrl - Callback url, e.g. 'http://127.0.0.1:3000'
|
||||||
* @fulfil {String} - dashboard login url
|
* @returns Dashboard login URL, e.g.:
|
||||||
* @returns {Promise}
|
* 'https://dashboard.balena-cloud.com/login/cli/http%253A%252F%252F127.0.0.1%253A59581%252Fauth'
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* utils.getDashboardLoginURL('http://127.0.0.1:3000').then (url) ->
|
|
||||||
* console.log(url)
|
|
||||||
*/
|
*/
|
||||||
export const getDashboardLoginURL = (callbackUrl: string) => {
|
export async function getDashboardLoginURL(
|
||||||
|
callbackUrl: string,
|
||||||
|
): Promise<string> {
|
||||||
// Encode percentages signs from the escaped url
|
// Encode percentages signs from the escaped url
|
||||||
// characters to avoid angular getting confused.
|
// characters to avoid angular getting confused.
|
||||||
callbackUrl = encodeURIComponent(callbackUrl).replace(/%/g, '%25');
|
callbackUrl = encodeURIComponent(callbackUrl).replace(/%/g, '%25');
|
||||||
|
|
||||||
return getBalenaSdk()
|
const [{ URL }, dashboardUrl] = await Promise.all([
|
||||||
.settings.get('dashboardUrl')
|
import('url'),
|
||||||
.then((dashboardUrl) =>
|
getBalenaSdk().settings.get('dashboardUrl'),
|
||||||
url.resolve(dashboardUrl, `/login/cli/${callbackUrl}`),
|
]);
|
||||||
);
|
return new URL(`/login/cli/${callbackUrl}`, dashboardUrl).href;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Log in using a token, but only if the token is valid
|
* Log in using a token, but only if the token is valid.
|
||||||
* @function
|
|
||||||
* @protected
|
|
||||||
*
|
*
|
||||||
* @description
|
|
||||||
* This function checks that the token is not only well-structured
|
* This function checks that the token is not only well-structured
|
||||||
* but that it also authenticates with the server successfully.
|
* but that it also authenticates with the server successfully.
|
||||||
*
|
*
|
||||||
* If authenticated, the token is persisted, if not then the previous
|
* If authenticated, the token is persisted, if not then the previous
|
||||||
* login state is restored.
|
* login state is restored.
|
||||||
*
|
*
|
||||||
* @param {String} token - session token or api key
|
* @param token - session token or api key
|
||||||
* @fulfil {Boolean} - whether the login was successful or not
|
* @returns whether the login was successful or not
|
||||||
* @returns {Promise}
|
|
||||||
*
|
|
||||||
* utils.loginIfTokenValid('...').then (loggedIn) ->
|
|
||||||
* if loggedIn
|
|
||||||
* console.log('Token is valid!')
|
|
||||||
*/
|
*/
|
||||||
export const loginIfTokenValid = async (token: string): Promise<boolean> => {
|
export async function loginIfTokenValid(token?: string): Promise<boolean> {
|
||||||
if (_.isEmpty(token?.trim())) {
|
token = (token || '').trim();
|
||||||
|
if (!token) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const balena = getBalenaSdk();
|
const balena = getBalenaSdk();
|
||||||
@ -86,4 +73,4 @@ export const loginIfTokenValid = async (token: string): Promise<boolean> => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return isLoggedIn;
|
return isLoggedIn;
|
||||||
};
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user