2017-11-23 12:49:47 +00:00
|
|
|
###
|
2018-10-19 14:38:50 +00:00
|
|
|
Copyright 2016 Balena
|
2017-11-23 12:49:47 +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.
|
|
|
|
###
|
|
|
|
|
|
|
|
###*
|
|
|
|
# @module auth
|
|
|
|
###
|
|
|
|
|
2017-12-12 16:29:15 +00:00
|
|
|
open = require('opn')
|
2018-10-19 14:38:50 +00:00
|
|
|
balena = require('balena-sdk').fromSharedOptions()
|
2017-11-23 12:49:47 +00:00
|
|
|
server = require('./server')
|
|
|
|
utils = require('./utils')
|
|
|
|
|
|
|
|
###*
|
2018-10-19 14:38:50 +00:00
|
|
|
# @summary Login to the balena CLI using the web dashboard
|
2017-11-23 12:49:47 +00:00
|
|
|
# @function
|
|
|
|
# @public
|
|
|
|
#
|
|
|
|
# @description
|
|
|
|
# This function opens the user's default browser and points it
|
2018-10-19 14:38:50 +00:00
|
|
|
# to the balena dashboard where the session token exchange will
|
2017-11-23 12:49:47 +00:00
|
|
|
# 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}")
|
|
|
|
###
|
|
|
|
exports.login = ->
|
|
|
|
options =
|
|
|
|
port: 8989
|
|
|
|
path: '/auth'
|
|
|
|
|
|
|
|
# Needs to be 127.0.0.1 not localhost, because the ip only is whitelisted
|
|
|
|
# from mixed content warnings (as the target of a form in the result page)
|
|
|
|
callbackUrl = "http://127.0.0.1:#{options.port}#{options.path}"
|
|
|
|
return utils.getDashboardLoginURL(callbackUrl).then (loginUrl) ->
|
|
|
|
|
|
|
|
# Leave a bit of time for the
|
|
|
|
# server to get up and runing
|
|
|
|
setTimeout ->
|
2018-02-16 16:28:14 +00:00
|
|
|
open(loginUrl, { wait: false })
|
2017-11-23 12:49:47 +00:00
|
|
|
, 1000
|
|
|
|
|
|
|
|
return server.awaitForToken(options)
|
2018-10-19 14:38:50 +00:00
|
|
|
.tap(balena.auth.loginWithToken)
|