Class: Eleyo::API::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/eleyo/api/auth.rb

Defined Under Namespace

Classes: AccessToken, InvalidClientError, InvalidGrantError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Auth

Returns a new instance of Auth.



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

def initialize(options = {})
  self.client_id = options[:client_id]
  self.client_secret = options[:client_secret]
  self.redirect_uri = options[:redirect_uri]
  self.js_callback = options[:js_callback]
  self. = options[:login_mechanism] || 'redirect'
  self.element = options[:element]
  self.district_subdomain = options[:district_subdomain]
  self.current_user_uuid = options[:current_user_uuid]

  raise API::InitializerError.new(:client_id, "can't be blank") if self.client_id.to_s.empty?
  raise API::InitializerError.new(:client_secret, "can't be blank") if self.client_secret.to_s.empty?
  raise API::InitializerError.new(:redirect_uri, "can't be blank") if self.redirect_uri.to_s.empty?
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def client_secret
  @client_secret
end

#current_user_uuidObject

Returns the value of attribute current_user_uuid.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def current_user_uuid
  @current_user_uuid
end

#district_subdomainObject

Returns the value of attribute district_subdomain.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def district_subdomain
  @district_subdomain
end

#elementObject

Returns the value of attribute element.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def element
  @element
end

#js_callbackObject

Returns the value of attribute js_callback.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def js_callback
  @js_callback
end

#login_mechanismObject

Returns the value of attribute login_mechanism.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def 
  @login_mechanism
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



38
39
40
# File 'lib/eleyo/api/auth.rb', line 38

def redirect_uri
  @redirect_uri
end

Class Method Details

.server_uriObject



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

def self.server_uri
  if Eleyo::API.standardmode?
    "https://sso.reg.eleyo.com"
  elsif Eleyo::API.testmode?
    "https://sso.reg.eleyo.green"
  elsif Eleyo::API.devmode?
    "https://sso.#{HOSTNAME}"
  end
end

Instance Method Details

#access_token(code) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/eleyo/api/auth.rb', line 63

def access_token(code)
  data = {
    :code => code,
    :grant_type => "authorization_code",
    :redirect_uri => self.redirect_uri,
    :client_secret => self.client_secret,
    :client_id => self.client_id
  }

  request = HTTPI::Request.new
  request.url = "#{self.class.server_uri}/access_token"
  request.body = data
  request.headers = {'User-Agent' => USER_AGENT}

  response = HTTPI.post(request)

  if !response.error?
    return AccessToken.new(:token => JSON.parse(response.body)['access_token'], :auth => self)
  else
    begin
      resp_data = JSON.parse(response.body)
    rescue
    end
    if resp_data and resp_data["error"] == "invalid_grant"
      raise(API::Auth::InvalidGrantError.new(response.code, response.body))
    elsif resp_data and resp_data["error"] == "invalid_client"
      raise(API::Auth::InvalidClientError.new(response.code, response.body))
    else
      raise(API::Error.new(response.code, response.body))
    end
  end
end

#authorization_urlObject



55
56
57
# File 'lib/eleyo/api/auth.rb', line 55

def authorization_url
  %(#{self.class.server_uri}/authorize?client_id=#{self.client_id}&redirect_uri=#{self.redirect_uri}&district=#{self.district_subdomain})
end

#javascriptObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/eleyo/api/auth.rb', line 96

def javascript
  options = {
    district: self.district_subdomain,
    element: self.element,
    login: {
      current_uuid: self.current_user_uuid,
      client_id: self.client_id,
      redirect_uri: self.redirect_uri,
      login_mechanism: self.,
      callback: self.js_callback
    }
  }
  return %(new SwitchBoardIOLogin(#{options.to_json});)
end

#registration_urlObject



59
60
61
# File 'lib/eleyo/api/auth.rb', line 59

def registration_url
  %(#{self.class.server_uri}/register?client_id=#{self.client_id}&redirect_uri=#{self.redirect_uri}&district=#{self.district_subdomain})
end