Class: Songkick::OAuth2::Provider::Authorization
- Inherits:
-
Object
- Object
- Songkick::OAuth2::Provider::Authorization
- Defined in:
- lib/songkick/oauth2/provider/authorization.rb
Constant Summary collapse
- REQUIRED_PARAMS =
[RESPONSE_TYPE, CLIENT_ID, REDIRECT_URI]
- VALID_PARAMS =
REQUIRED_PARAMS + [SCOPE, STATE]
- VALID_RESPONSES =
[CODE, TOKEN, CODE_AND_TOKEN]
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#error_description ⇒ Object
readonly
Returns the value of attribute error_description.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #deny_access! ⇒ Object
- #grant_access!(options = {}) ⇒ Object
-
#initialize(resource_owner, params, transport_error = nil) ⇒ Authorization
constructor
A new instance of Authorization.
- #params ⇒ Object
- #redirect? ⇒ Boolean
- #redirect_uri ⇒ Object
- #response_body ⇒ Object
- #response_headers ⇒ Object
- #response_status ⇒ Object
- #scopes ⇒ Object
- #unauthorized_scopes ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(resource_owner, params, transport_error = nil) ⇒ Authorization
Returns a new instance of Authorization.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 15 def initialize(resource_owner, params, transport_error = nil) @owner = resource_owner @params = params @scope = params[SCOPE] @state = params[STATE] @transport_error = transport_error validate! return unless @owner and not @error @model = @owner.(@client) return unless @model and @model.in_scope?(scopes) and not @model.expired? @authorized = true if @params[RESPONSE_TYPE] =~ /code/ @code = @model.generate_code end if @params[RESPONSE_TYPE] =~ /token/ @access_token = @model.generate_access_token end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def access_token @access_token end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def client @client end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def code @code end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def error @error end |
#error_description ⇒ Object (readonly)
Returns the value of attribute error_description.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def error_description @error_description end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def expires_in @expires_in end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def owner @owner end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
6 7 8 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 6 def refresh_token @refresh_token end |
Instance Method Details
#deny_access! ⇒ Object
68 69 70 71 72 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 68 def deny_access! @code = @access_token = @refresh_token = nil @error = ACCESS_DENIED @error_description = "The user denied you access" end |
#grant_access!(options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 50 def grant_access!( = {}) @model = Model::Authorization.for(@owner, @client, :response_type => @params[RESPONSE_TYPE], :scope => @scope, :duration => [:duration]) @code = @model.code @access_token = @model.access_token @refresh_token = @model.refresh_token @expires_in = @model.expires_in unless @params[RESPONSE_TYPE] == CODE @expires_in = @model.expires_in end @authorized = true end |
#params ⇒ Object
74 75 76 77 78 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 74 def params params = {} VALID_PARAMS.each { |key| params[key] = @params[key] if @params.has_key?(key) } params end |
#redirect? ⇒ Boolean
80 81 82 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 80 def redirect? @client and (@authorized or not valid?) end |
#redirect_uri ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 84 def redirect_uri return nil unless @client base_redirect_uri = @client.redirect_uri q = (base_redirect_uri =~ /\?/) ? '&' : '?' if not valid? query = to_query_string(ERROR, ERROR_DESCRIPTION, STATE) "#{ base_redirect_uri }#{ q }#{ query }" elsif @params[RESPONSE_TYPE] == CODE_AND_TOKEN query = to_query_string(CODE, STATE) fragment = to_query_string(ACCESS_TOKEN, EXPIRES_IN, SCOPE) "#{ base_redirect_uri }#{ query.empty? ? '' : q + query }##{ fragment }" elsif @params[RESPONSE_TYPE] == TOKEN fragment = to_query_string(ACCESS_TOKEN, EXPIRES_IN, SCOPE, STATE) "#{ base_redirect_uri }##{ fragment }" else query = to_query_string(CODE, SCOPE, STATE) "#{ base_redirect_uri }#{ q }#{ query }" end end |
#response_body ⇒ Object
108 109 110 111 112 113 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 108 def response_body warn "Songkick::OAuth2::Provider::Authorization no longer returns a response body "+ "when the request is invalid. You should call valid? to determine "+ "whether to render your login page or an error page." nil end |
#response_headers ⇒ Object
115 116 117 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 115 def response_headers redirect? ? {} : {'Cache-Control' => 'no-store'} end |
#response_status ⇒ Object
119 120 121 122 123 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 119 def response_status return 302 if redirect? return 200 if valid? @client ? 302 : 400 end |
#scopes ⇒ Object
41 42 43 44 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 41 def scopes scopes = @scope ? @scope.split(/\s+/).delete_if { |s| s.empty? } : [] Set.new(scopes) end |
#unauthorized_scopes ⇒ Object
46 47 48 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 46 def @model ? scopes.select { |s| not @model.in_scope?(s) } : scopes end |
#valid? ⇒ Boolean
125 126 127 |
# File 'lib/songkick/oauth2/provider/authorization.rb', line 125 def valid? @error.nil? end |