Class: RSimperium::Auth

Inherits:
Object
  • Object
show all
Includes:
ApiHelpers
Defined in:
lib/r_simperium/auth.rb

Overview

Example use:

require 'r_simperium'
auth = RSimprium::Auth.new 'myapp', 'cbbae31841ac4d44a93cd82081a5b74f'
access_token = auth.create '[email protected]', 'secret123'

Instance Method Summary collapse

Methods included from ApiHelpers

#api_auth_header, #api_meta, #api_request, #api_request_with_auth, #api_url

Constructor Details

#initialize(app_id, api_key, host = nil, scheme = 'https') ⇒ Auth

A new Auth object that creates and authorizes Simperium users.

Parameters:

  • app_id (String)

    Your Simperium app id

  • api_key (String)

    Your Simperium app api key

  • host (String) (defaults to: nil)

    The host to send requests to

  • scheme (String) (defaults to: 'https')

    The scheme to use for requests



16
17
18
19
20
21
22
# File 'lib/r_simperium/auth.rb', line 16

def initialize(app_id, api_key, host=nil, scheme='https')
  host ||= ENV['SIMPERIUM_AUTHHOST'] || 'auth.simperium.com'
  @app_id = app_id
  @api_key = api_key
  @host = host
  @scheme = scheme
end

Instance Method Details

#authorize(username, password) ⇒ String

Authorizes an existing Simperium user.

Parameters:

  • username (String)

    The user’s username

  • password (String)

    The user’s password

Returns:

  • (String)

    The user’s access token



28
29
30
31
32
33
34
35
# File 'lib/r_simperium/auth.rb', line 28

def authorize(username, password)
  data = {
    :clientid => @api_key,
    :username => username,
    :password => password, }
  response = api_request_with_auth :post, api_url('/authorize/'), :payload => data
  response.data['access_token']
end

#change_password(username, new_password) ⇒ boolean

Resets a Simperium user’s password

Parameters:

  • username (String)

    The user’s username

  • password (String)

    The user’s new password (optional)

Returns:

  • (boolean)

    True if reset was a success



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/r_simperium/auth.rb', line 59

def change_password(username, new_password)
  data = {
      :username => username,
      :new_password => new_password
  }
  begin
    response = api_request_with_auth :post, api_url('/reset_password/'), :payload => data
    return response.data['status'] == 'success'
  rescue RestClient::Exception => e
    puts "The api request failed: #{e.message}"
    false
  end
end

#create(username, password) ⇒ String

Creates a new Simperium user and returns its access token.

Parameters:

  • username (String)

    The new user’s username

  • password (String)

    The new user’s password

Returns:

  • (String)

    The new user’s access token



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/r_simperium/auth.rb', line 41

def create(username, password)
  data = {
      :clientid => @api_key,
      :username => username,
      :password => password, }
  begin
    response = api_request_with_auth :post, api_url('/create/'), :payload => data
    response.data['access_token']
  rescue RestClient::Exception => e
    puts "The api request failed: #{e.message}"
    nil
  end
end

#delete(username) ⇒ Object

Deletes a Simperium user and all user data. Requires an API key with admin privileges.

Parameters:

  • username (String)

    The new user’s username



76
77
78
79
80
81
# File 'lib/r_simperium/auth.rb', line 76

def delete(username)
  data = {
      :clientid => @api_key,
      :username => username}
  api_request_with_auth :post, api_url('/delete/'), :payload => data
end