Class: Sorcery::Providers::Discord

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth2
Defined in:
lib/sorcery/providers/discord.rb

Overview

This class adds support for OAuth with discordapp.com

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#authorize_url, #build_client, #get_access_token, #oauth_version

Methods inherited from Base

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializeDiscord

Returns a new instance of Discord.



10
11
12
13
14
15
16
17
18
19
# File 'lib/sorcery/providers/discord.rb', line 10

def initialize
  super

  @scope          = 'identify'
  @site           = 'https://discordapp.com/'
  @auth_path      = '/api/oauth2/authorize'
  @token_url      = '/api/oauth2/token'
  @user_info_path = '/api/users/@me'
  @state          = SecureRandom.hex(16)
end

Instance Attribute Details

#auth_pathObject

Returns the value of attribute auth_path.



8
9
10
# File 'lib/sorcery/providers/discord.rb', line 8

def auth_path
  @auth_path
end

#scopeObject

Returns the value of attribute scope.



8
9
10
# File 'lib/sorcery/providers/discord.rb', line 8

def scope
  @scope
end

#token_urlObject

Returns the value of attribute token_url.



8
9
10
# File 'lib/sorcery/providers/discord.rb', line 8

def token_url
  @token_url
end

#user_info_pathObject

Returns the value of attribute user_info_path.



8
9
10
# File 'lib/sorcery/providers/discord.rb', line 8

def 
  @user_info_path
end

Instance Method Details

#get_user_hash(access_token) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/sorcery/providers/discord.rb', line 21

def get_user_hash(access_token)
  response = access_token.get()
  body = JSON.parse(response.body)
  auth_hash(access_token).tap do |h|
    h[:user_info] = body
    h[:uid] = body['id']
  end
end

#login_url(_params, _session) ⇒ Object

calculates and returns the url to which the user should be redirected, to get authenticated at the external provider’s site.



32
33
34
# File 'lib/sorcery/providers/discord.rb', line 32

def (_params, _session)
  authorize_url(authorize_url: auth_path)
end

#process_callback(params, _session) ⇒ Object

tries to login the user from access token



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sorcery/providers/discord.rb', line 37

def process_callback(params, _session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end
  get_access_token(
    args,
    token_url: token_url,
    client_id: @key,
    client_secret: @secret,
    grant_type: 'authorization_code',
    token_method: :post
  )
end