Class: AruxApp::API::Auth
- Inherits:
-
Object
- Object
- AruxApp::API::Auth
- Defined in:
- lib/arux_app/api/auth.rb
Defined Under Namespace
Classes: AccessToken, InvalidClientError, InvalidGrantError
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#current_user_uuid ⇒ Object
Returns the value of attribute current_user_uuid.
-
#district_subdomain ⇒ Object
Returns the value of attribute district_subdomain.
-
#element ⇒ Object
Returns the value of attribute element.
-
#js_callback ⇒ Object
Returns the value of attribute js_callback.
-
#login_mechanism ⇒ Object
Returns the value of attribute login_mechanism.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
Class Method Summary collapse
Instance Method Summary collapse
- #access_token(code) ⇒ Object
- #api_uri ⇒ Object
- #authorization_url(scope: "public") ⇒ Object
- #basic_authentication(username, password, scope = "public") ⇒ Object
- #client_credentials_token ⇒ Object
-
#initialize(options = {}) ⇒ Auth
constructor
A new instance of Auth.
- #javascript ⇒ Object
- #public_uri ⇒ Object
- #registration_url ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Auth
Returns a new instance of Auth.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/arux_app/api/auth.rb', line 31 def initialize( = {}) self.client_id = [:client_id] self.client_secret = [:client_secret] self.redirect_uri = [:redirect_uri] self.js_callback = [:js_callback] self.login_mechanism = [:login_mechanism] || 'redirect' self.element = [:element] self.district_subdomain = [:district_subdomain] self.current_user_uuid = [:current_user_uuid] self.api_key = [:api_key] 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
#api_key ⇒ Object
Returns the value of attribute api_key.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def api_key @api_key end |
#client_id ⇒ Object
Returns the value of attribute client_id.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def client_secret @client_secret end |
#current_user_uuid ⇒ Object
Returns the value of attribute current_user_uuid.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def current_user_uuid @current_user_uuid end |
#district_subdomain ⇒ Object
Returns the value of attribute district_subdomain.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def district_subdomain @district_subdomain end |
#element ⇒ Object
Returns the value of attribute element.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def element @element end |
#js_callback ⇒ Object
Returns the value of attribute js_callback.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def js_callback @js_callback end |
#login_mechanism ⇒ Object
Returns the value of attribute login_mechanism.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def login_mechanism @login_mechanism end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
29 30 31 |
# File 'lib/arux_app/api/auth.rb', line 29 def redirect_uri @redirect_uri end |
Class Method Details
Instance Method Details
#access_token(code) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/arux_app/api/auth.rb', line 105 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 = "#{api_uri}/oauth/token" request.body = data request.headers = {'User-Agent' => USER_AGENT} response = HTTPI.post(request) if !response.error? AccessToken.new( token: JSON.parse(response.body)['access_token'], scope: JSON.parse(response.body)['scope'], 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 |
#api_uri ⇒ Object
59 60 61 |
# File 'lib/arux_app/api/auth.rb', line 59 def api_uri self.class.api_uri end |
#authorization_url(scope: "public") ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/arux_app/api/auth.rb', line 63 def (scope: "public") base_uri = URI.parse("#{public_uri}/oauth/authorize") params = { scope: scope, response_type: "code", client_id: client_id, redirect_uri: redirect_uri, district: district_subdomain } base_uri.query = URI.encode_www_form(params) base_uri.to_s end |
#basic_authentication(username, password, scope = "public") ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/arux_app/api/auth.rb', line 76 def basic_authentication(username, password, scope = "public") params = { scope: scope, grant_type: "password", client_id: client_id, client_secret: client_secret } request = HTTPI::Request.new.tap do |req| req.url = "#{public_uri}/oauth/token" req.body = params req.headers = { 'User-Agent' => USER_AGENT } req.auth.basic(username, password) end response = HTTPI.post(request) raise(API::Error.new(response.code, response.body)) if response.error? AccessToken.new( token: JSON.parse(response.body)['access_token'], scope: JSON.parse(response.body)['scope'], auth: self ) end |
#client_credentials_token ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/arux_app/api/auth.rb', line 142 def client_credentials_token data = { scope: "public", grant_type: "client_credentials", client_id: client_id, client_secret: client_secret } request = HTTPI::Request.new request.url = "#{api_uri}/oauth/token" request.body = data request.headers = {'User-Agent' => USER_AGENT} response = HTTPI.post(request) if !response.error? AccessToken.new(:token => JSON.parse(response.body)['access_token'], auth: self) else raise(API::Error.new(response.code, response.body)) end end |
#javascript ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/arux_app/api/auth.rb', line 163 def javascript = { 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.login_mechanism, callback: self.js_callback } } return %(new SwitchBoardIOLogin(#{.to_json});) end |
#public_uri ⇒ Object
51 52 53 |
# File 'lib/arux_app/api/auth.rb', line 51 def public_uri self.class.public_uri end |
#registration_url ⇒ Object
101 102 103 |
# File 'lib/arux_app/api/auth.rb', line 101 def registration_url %(#{public_uri}/users/sign_up?district=#{self.district_subdomain}) end |