Class: Keywright::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/keywright/authorization.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) {|_self| ... } ⇒ Authorization

Returns a new instance of Authorization.

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
# File 'lib/keywright/authorization.rb', line 15

def initialize( configuration = nil )
  @configuration = ( configuration || Keywright.configuration )&.to_h
  yield self if block_given?
end

Class Method Details

.code_from_uri(uri) ⇒ Object



6
7
8
9
10
11
# File 'lib/keywright/authorization.rb', line 6

def code_from_uri( uri )
  uri = URI.parse( uri )
  query_string = uri.query
  params = UriHelpers.parse_query( query_string )
  params[ 'code' ]
end

Instance Method Details

#code_from_uri(url) ⇒ Object



56
57
58
# File 'lib/keywright/authorization.rb', line 56

def code_from_uri( url )
  self.class.code_from_uri( url )
end

#url(**options) ⇒ Object

The url method will return a url, with the required parameters including the client id, redirect_uri, scopes and state, that the user agent should be redirected to for authorization.

The authorization_uri and client_id are required. These must be provided through the options or previously assigned to the instance configuration or global configuration.

The redirect_uri, scopes, and state parameters are optional. If the redirect_uri is blank it will not be included. If the scopes are blank the scopes from the configuration will be used ( if any ). If the state is blank it will not be included.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/keywright/authorization.rb', line 32

def url( **options )
  authorization_uri = options[ :authorization_uri ] || @configuration[ :authorization_uri ]
  raise ArgumentError.new( 'The authorization_uri must be provided or configured.' ) \
    unless UriHelpers.valid?( authorization_uri )

  client_id = options[ :client_id ] || @configuration[ :client_id ]
  raise ArgumentError.new( 'The client_id must be provided or configured.' ) \
    if client_id.nil?

  uri = URI.parse( authorization_uri )
  params = UriHelpers.parse_query( uri.query )

  params = params.merge( { 
    'client_id' => client_id,

    'redirect_uri' => options[ :redirect_uri ],
    'scope' => options[ :scopes ] || @configuration.scopes,
    'state' => options[ :state ]
  }.compact )

  uri.query = URI.encode_www_form( params )
  uri.to_s
end