Class: YiffSpace::Auth::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yiffspace/auth/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/yiffspace/auth/client.rb', line 14

def initialize(name)
  @name                    = name.to_sym
  @server_url              = "https://auth.yiff.space"
  @redirect_uri            = "http://127.0.0.1:3000/auth/cb"
  @scopes                  = %i[openid discord offline_access entitlements]
  @auth_session_key        = :"yiffspace_auth_#{name}"
  @user_session_key        = :"yiffspace_user_#{name}"
  @token_session_key       = :"yiffspace_token_#{name}"
  @return_path_session_key = :"yiffspace_return_path_#{name}"
  @state_session_key       = :"yiffspace_state_#{name}"
  @state_generator         = -> { SecureRandom.hex(48) }
  @update_discord_images   = true
end

Instance Attribute Details

#after_auth_actionObject

Returns the value of attribute after_auth_action.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def after_auth_action
  @after_auth_action
end

#after_logout_actionObject

Returns the value of attribute after_logout_action.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def after_logout_action
  @after_logout_action
end

#auth_session_keyObject

Returns the value of attribute auth_session_key.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def auth_session_key
  @auth_session_key
end

#client_idObject

Returns the value of attribute client_id.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def client_id
  @client_id
end

#client_nameObject

Returns the value of attribute client_name.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def client_name
  @client_name
end

#client_secretObject

Returns the value of attribute client_secret.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def client_secret
  @client_secret
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/yiffspace/auth/client.rb', line 12

def name
  @name
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def redirect_uri
  @redirect_uri
end

#return_path_session_keyObject

Returns the value of attribute return_path_session_key.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def return_path_session_key
  @return_path_session_key
end

#scopesObject

Returns the value of attribute scopes.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def scopes
  @scopes
end

#server_urlObject

Returns the value of attribute server_url.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def server_url
  @server_url
end

#state_generatorObject

Returns the value of attribute state_generator.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def state_generator
  @state_generator
end

#state_session_keyObject

Returns the value of attribute state_session_key.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def state_session_key
  @state_session_key
end

#token_session_keyObject

Returns the value of attribute token_session_key.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def token_session_key
  @token_session_key
end

#update_discord_imagesObject

Returns the value of attribute update_discord_images.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def update_discord_images
  @update_discord_images
end

#user_session_keyObject

Returns the value of attribute user_session_key.



6
7
8
# File 'lib/yiffspace/auth/client.rb', line 6

def user_session_key
  @user_session_key
end

Instance Method Details

#exchange(code) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/yiffspace/auth/client.rb', line 47

def exchange(code)
  oidc_client.authorization_code = code
  token                          = oidc_client.access_token!
  user                           = token.userinfo!
  id                             = user.raw_attributes["discord"]["id"]
  authinfo                       = AuthInfo.new(token: token, entitlements: user.raw_attributes["entitlements"], roles: user.raw_attributes["roles"], id: id)
  userinfo                       = UserInfo.new(user: user, id: id, discord: user.raw_attributes["discord"], client_id: oidc_client.identifier)
  Auth::ExchangeResponse.new(authinfo, userinfo)
end

#oidc_clientObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/yiffspace/auth/client.rb', line 32

def oidc_client
  @oidc_client ||= OpenIDConnect::Client.new(
    identifier:             client_id,
    secret:                 client_secret,
    redirect_uri:           redirect_uri,
    authorization_endpoint: openid_config.authorization_endpoint,
    token_endpoint:         openid_config.token_endpoint,
    userinfo_endpoint:      openid_config.userinfo_endpoint,
  )
end

#openid_configObject



28
29
30
# File 'lib/yiffspace/auth/client.rb', line 28

def openid_config
  @openid_config ||= OpenIDConnect::Discovery::Provider::Config.discover!("#{server_url}/application/o/#{client_name}/")
end

#url(state: nil) ⇒ Object



43
44
45
# File 'lib/yiffspace/auth/client.rb', line 43

def url(state: nil)
  oidc_client.authorization_uri(scope: scopes, state: state)
end