Module: Authentication

Included in:
Discogs::Wrapper
Defined in:
lib/wrapper/authentication.rb

Instance Method Summary collapse

Instance Method Details

#any_authentication?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/wrapper/authentication.rb', line 26

def any_authentication?
  user_facing? or self_authenticating?
end

#auth_paramsObject



5
6
7
8
9
10
11
# File 'lib/wrapper/authentication.rb', line 5

def auth_params
  if self_authenticating?
    {:token => @user_token}
  else
    {}
  end
end

#authenticate(request_token, verifier) ⇒ OAuth::AccessToken

Retrieves an OAuth access token from the Discogs server.

Parameters:

  • request_token (OAuth::RequestToken)

    request token

  • verifier (String)

    verifier token

Returns:

  • (OAuth::AccessToken)


52
53
54
55
56
57
58
# File 'lib/wrapper/authentication.rb', line 52

def authenticate(request_token, verifier)
  if request_token.is_a?(OAuth::RequestToken)
    @access_token = request_token.get_access_token(:oauth_verifier => verifier)
  else
    raise OAuth::Error, "Invalid Request Token"
  end
end

#authenticated?(username = nil, &block) ⇒ Boolean

Returns true if an OAuth access_token is present. If a username is given, the authenticated username must match it.

Parameters:

  • username (String) (defaults to: nil)

    username

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wrapper/authentication.rb', line 64

def authenticated?(username=nil, &block)
  auth = if username
    any_authentication? and authenticated_username == username
  else
    any_authentication?
  end

  if block_given?
    auth ? yield : raise_authentication_error
  else
    auth
  end
end

#get_request_token(app_key, app_secret, callback) ⇒ Hash

Retrieves an OAuth request token from the Discogs server.

Parameters:

  • app_key (String)

    application id

  • app_secret (String)

    application secret

Returns:

  • (Hash)

    containing a :request_token that should be stored locally and a :authorize_url that the user must browse to.



36
37
38
39
40
41
42
43
44
# File 'lib/wrapper/authentication.rb', line 36

def get_request_token(app_key, app_secret, callback)
  consumer = OAuth::Consumer.new(app_key, app_secret,
               :authorize_url => "https://www.discogs.com/oauth/authorize",
               :site          => "https://api.discogs.com")
  request_token = consumer.get_request_token(:oauth_callback => callback)

  {:request_token => request_token,
   :authorize_url => request_token.authorize_url(:oauth_callback => callback)}
end

#self_authenticating?Boolean

Indicates whether this instance is self-authenticated.

Returns:

  • (Boolean)


21
22
23
# File 'lib/wrapper/authentication.rb', line 21

def self_authenticating?
  !!@user_token
end

#user_facing?Boolean

Indicates whether this instance is authenticated as a user-facing app.

Returns:

  • (Boolean)


15
16
17
# File 'lib/wrapper/authentication.rb', line 15

def user_facing?
  !!@access_token
end