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.
|
|
|
|
###
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
balena = require('balena-sdk').fromSharedOptions()
|
2017-11-23 12:49:47 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
url = require('url')
|
|
|
|
Promise = require('bluebird')
|
|
|
|
|
|
|
|
###*
|
|
|
|
# @summary Get dashboard CLI login URL
|
|
|
|
# @function
|
|
|
|
# @protected
|
|
|
|
#
|
|
|
|
# @param {String} callbackUrl - callback url
|
|
|
|
# @fulfil {String} - dashboard login url
|
|
|
|
# @returns {Promise}
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# utils.getDashboardLoginURL('http://127.0.0.1:3000').then (url) ->
|
|
|
|
# console.log(url)
|
|
|
|
###
|
|
|
|
exports.getDashboardLoginURL = (callbackUrl) ->
|
|
|
|
|
|
|
|
# Encode percentages signs from the escaped url
|
|
|
|
# characters to avoid angular getting confused.
|
|
|
|
callbackUrl = encodeURIComponent(callbackUrl).replace(/%/g, '%25')
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
balena.settings.get('dashboardUrl').then (dashboardUrl) ->
|
2017-11-23 12:49:47 +00:00
|
|
|
return url.resolve(dashboardUrl, "/login/cli/#{callbackUrl}")
|
|
|
|
|
|
|
|
###*
|
2018-03-28 18:13:12 +00:00
|
|
|
# @summary Log in using a token, but only if the token is valid
|
2017-11-23 12:49:47 +00:00
|
|
|
# @function
|
|
|
|
# @protected
|
|
|
|
#
|
|
|
|
# @description
|
|
|
|
# This function checks that the token is not only well-structured
|
|
|
|
# but that it also authenticates with the server successfully.
|
|
|
|
#
|
2018-03-28 18:13:12 +00:00
|
|
|
# If authenticated, the token is persisted, if not then the previous
|
|
|
|
# login state is restored.
|
|
|
|
#
|
|
|
|
# @param {String} token - session token or api key
|
|
|
|
# @fulfil {Boolean} - whether the login was successful or not
|
2017-11-23 12:49:47 +00:00
|
|
|
# @returns {Promise}
|
|
|
|
#
|
2018-03-28 18:13:12 +00:00
|
|
|
# utils.loginIfTokenValid('...').then (loggedIn) ->
|
|
|
|
# if loggedIn
|
2017-11-23 12:49:47 +00:00
|
|
|
# console.log('Token is valid!')
|
|
|
|
###
|
2018-03-28 18:13:12 +00:00
|
|
|
exports.loginIfTokenValid = (token) ->
|
|
|
|
if not token? or _.isEmpty(token.trim())
|
2017-11-23 12:49:47 +00:00
|
|
|
return Promise.resolve(false)
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.auth.getToken()
|
2018-03-23 15:35:39 +00:00
|
|
|
.catchReturn(undefined)
|
|
|
|
.then (currentToken) ->
|
2018-10-19 14:38:50 +00:00
|
|
|
balena.auth.loginWithToken(token)
|
2018-03-28 18:13:12 +00:00
|
|
|
.return(token)
|
2018-10-19 14:38:50 +00:00
|
|
|
.then(balena.auth.isLoggedIn)
|
2017-11-23 12:49:47 +00:00
|
|
|
.tap (isLoggedIn) ->
|
|
|
|
return if isLoggedIn
|
|
|
|
|
|
|
|
if currentToken?
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.auth.loginWithToken(currentToken)
|
2017-11-23 12:49:47 +00:00
|
|
|
else
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.auth.logout()
|