balena-cli/lib/resin/token/token.coffee

69 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2014-11-26 16:56:52 +00:00
data = require('../data/data')
2014-12-08 12:48:50 +00:00
# @nodoc
2014-11-14 18:28:58 +00:00
TOKEN_KEY = 'token'
2014-12-08 12:48:50 +00:00
# Save token
#
# The token is saved to $(dataPrefix)/token, which usually equals to $HOME/.resin/token
#
# @param {String} newToken the token
# @param {Function} callback callback(error)
#
# @note The token is saved as plain text.
#
# @todo We should make the token more secure
#
# @example Save Token
# resin.token.saveToken myToken, (error) ->
# throw error if error?
#
exports.saveToken = (newToken, callback) ->
2014-11-14 18:28:58 +00:00
data.set(TOKEN_KEY, newToken, encoding: 'utf8', callback)
2014-12-08 12:48:50 +00:00
# Check if we have any token saved
#
# @param {Function} callback callback(hasToken)
#
# @example Has Token
# resin.token.hasToken (hasToken) ->
# if hasToken
# console.log('It\'s there!')
# else
# console.log('It\'s not there!')
#
exports.hasToken = (callback) ->
2014-11-14 18:28:58 +00:00
data.has(TOKEN_KEY, callback)
2014-12-08 12:48:50 +00:00
# Get saved token value
#
# @param {Function} callback callback(error, token)
#
# @note If the key doesn't exist, undefined and no error is returned
#
# @example Get token
# resin.token.getToken (error, token) ->
# throw error if error?
# if token?
# console.log("My token is: #{token}")
#
exports.getToken = (callback) ->
2014-11-14 18:28:58 +00:00
data.get(TOKEN_KEY, encoding: 'utf8', callback)
2014-12-08 12:48:50 +00:00
# Remove token from the filesystem
#
# @param {Function} callback callback(error)
#
# @note If the token doesn't exist, no action is performed
#
# @example Clear Token
# resin.token.clearToken (error) ->
# throw error if error?
#
exports.clearToken = (callback) ->
2014-11-14 18:28:58 +00:00
data.has TOKEN_KEY, (hasToken) ->
if hasToken
return data.remove(TOKEN_KEY, callback)
else
return callback?(null)