2020-02-05 21:25:56 +00:00
|
|
|
/*
|
2020-05-15 21:21:32 +00:00
|
|
|
Copyright 2016-2020 Balena
|
2020-02-05 21:25:56 +00:00
|
|
|
|
|
|
|
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-08-01 00:07:43 +00:00
|
|
|
import { LoginServer } from './server';
|
2020-02-27 14:55:30 +00:00
|
|
|
|
2020-02-05 21:25:56 +00:00
|
|
|
/**
|
|
|
|
* @module auth
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-09-18 14:35:36 +00:00
|
|
|
* @summary Login to balenaCLI using the web dashboard
|
2020-02-05 21:25:56 +00:00
|
|
|
* @function
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* This function opens the user's default browser and points it
|
|
|
|
* to the balena dashboard where the session token exchange will
|
|
|
|
* take place.
|
|
|
|
*
|
|
|
|
* Once the the token is retrieved, it's automatically persisted.
|
|
|
|
*
|
|
|
|
* @fulfil {String} - session token
|
|
|
|
* @returns {Promise}
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* auth.login().then (sessionToken) ->
|
|
|
|
* console.log('I\'m logged in!')
|
|
|
|
* console.log("My session token is: #{sessionToken}")
|
|
|
|
*/
|
2020-08-01 00:07:43 +00:00
|
|
|
export async function login({ host = '127.0.0.1', port = 0 }) {
|
2020-02-05 21:25:56 +00:00
|
|
|
const utils = await import('./utils');
|
|
|
|
|
2020-08-01 00:07:43 +00:00
|
|
|
const loginServer = new LoginServer();
|
|
|
|
const {
|
|
|
|
host: actualHost,
|
|
|
|
port: actualPort,
|
|
|
|
urlPath,
|
|
|
|
} = await loginServer.start({ host, port });
|
2020-02-05 21:25:56 +00:00
|
|
|
|
2020-08-01 00:07:43 +00:00
|
|
|
const callbackUrl = `http://${actualHost}:${actualPort}${urlPath}`;
|
2020-02-05 21:25:56 +00:00
|
|
|
const loginUrl = await utils.getDashboardLoginURL(callbackUrl);
|
2020-08-01 00:07:43 +00:00
|
|
|
|
2020-05-15 21:21:32 +00:00
|
|
|
console.info(`Opening web browser for URL:\n${loginUrl}`);
|
2020-08-01 00:07:43 +00:00
|
|
|
const open = await import('open');
|
|
|
|
open(loginUrl, { wait: false });
|
2020-02-05 21:25:56 +00:00
|
|
|
|
2020-02-27 14:55:30 +00:00
|
|
|
const balena = getBalenaSdk();
|
2020-08-01 00:07:43 +00:00
|
|
|
const token = await loginServer.awaitForToken();
|
2020-06-30 20:24:35 +00:00
|
|
|
await balena.auth.loginWithToken(token);
|
2020-08-01 00:07:43 +00:00
|
|
|
loginServer.shutdown();
|
2020-06-30 20:32:11 +00:00
|
|
|
return token;
|
2020-08-01 00:07:43 +00:00
|
|
|
}
|