module Particle::Client::Tokens

Client methods for the Particle authentication/token API

@see docs.particle.io/core/api/#introduction-authentication

Public Instance Methods

create_token(username, password, options = {}) click to toggle source

Authenticate with Particle and create a new token

@param username [String] The username (email) used to log in to

the Particle Cloud API

@param password [String] The password used to log in to

the Particle Cloud API

@param options [Hash] Optional Particle Cloud API options to

create the token.
:expires_in => How many seconds should the token last for?
               0 means a token that never expires
:expires_at => Date and time when should the token expire
:client => Particle OAuth client to use.
           Defaults to 'particle'
:secret => Corresponding OAuth secret to use.
           Defaults to 'particle'
:grant_type => Type of OAuth authentication flow to use
               Defaults to 'password'

@return [Token] The token object

# File lib/particle/client/tokens.rb, line 59
def create_token(username, password, options = {})
  client = options.delete(:client) { 'particle' }
  secret = options.delete(:secret) { 'particle' }
  data = URI.encode_www_form({
    grant_type: 'password',
    username: username,
    password: password
  }.merge(options))
  http_options = {
    headers: { content_type: "application/x-www-form-urlencoded" },
    username: client,
    password: secret
  }
  result = request(:post, Token.create_path, data, http_options)
  result[:token] = result.delete(:access_token)
  token(result)
end
login(username, password, options = {}) click to toggle source

Authenticate with Particle and start using the token on the client right away

@param username [String] The username (email) used to log in to

the Particle Cloud API

@param password [String] The password used to log in to

the Particle Cloud API

@param options [Hash] Additional Particle Cloud API options to

create the token.

@return [Token] The token object

# File lib/particle/client/tokens.rb, line 87
def login(username, password, options = {})
  token = create_token(username, password, options)
  self.access_token = token
  token
end
remove_token(username, password, target) click to toggle source

Remove a Particle token

@param username [String] The username (email) used to log in to

the Particle Cloud API

@param password [String] The password used to log in to

the Particle Cloud API

@param target [String, Token] An token id or {Token} object @return [boolean] true for success

# File lib/particle/client/tokens.rb, line 101
def remove_token(username, password, target)
  http_options = {
    username: username,
    password: password
  }
  result = request(:delete, token(target).path, "", http_options)
  result[:ok]
end
token(target = {}) click to toggle source

Create a domain model for a Particle token

@param target [String, Hash, Token] A token id, hash of attributes or {Token} object @return [Token] A token object to interact with

# File lib/particle/client/tokens.rb, line 16
def token(target = {})
  if target.is_a? Token
    target
  else
    Token.new(self, target)
  end
end
tokens(username, password) click to toggle source

List all Particle tokens for the account

@param username [String] The username (email) used to log in to

the Particle Cloud API

@param password [String] The password used to log in to

the Particle Cloud API

@return [Array<Token>] List of Particle tokens

# File lib/particle/client/tokens.rb, line 31
def tokens(username, password)
  http_options = {
    username: username,
    password: password
  }
  request(:get, Token.list_path, "", http_options).map do |attributes|
    token(attributes)
  end
end