Skip to content

Commit 0482093

Browse files
committed
implement oidc as a new provider
1 parent 4fe67cc commit 0482093

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

lib/code0/identities.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require_relative "identities/provider/google"
1111
require_relative "identities/provider/discord"
1212
require_relative "identities/provider/github"
13+
require_relative "identities/provider/oidc"
1314

1415
module Code0
1516
module Identities

lib/code0/identities/provider/oidc.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
module Code0
4+
module Identities
5+
module Provider
6+
class Oidc < BaseOauth
7+
def token_url
8+
config[:token_url]
9+
end
10+
11+
def token_payload(code)
12+
{ code: code,
13+
grant_type: "authorization_code",
14+
redirect_uri: config[:redirect_uri],
15+
client_id: config[:client_id],
16+
client_secret: config[:client_secret] }
17+
end
18+
19+
def user_details_url
20+
config[:user_details_url]
21+
end
22+
23+
def authorization_url
24+
config[:user_details_url]
25+
end
26+
27+
def create_identity(response, *)
28+
body = response.parsed_response
29+
30+
Identity.new(config[:provider_name],
31+
find_attribute(body, config[:attribute_statements][:identifier]),
32+
find_attribute(body, config[:attribute_statements][:username]),
33+
find_attribute(body, config[:attribute_statements][:email]),
34+
find_attribute(body, config[:attribute_statements][:firstname]),
35+
find_attribute(body, config[:attribute_statements][:lastname]))
36+
end
37+
38+
def config
39+
config = super
40+
41+
# rubocop:disable Layout/LineLength
42+
config[:provider_name] ||= :oidc
43+
config[:attribute_statements] ||= {}
44+
config[:attribute_statements][:identifier] ||= %w[sub id identifier]
45+
config[:attribute_statements][:username] ||= %w[username name login]
46+
config[:attribute_statements][:email] ||= %w[email mail]
47+
config[:attribute_statements][:firstname] ||= %w[first_name firstname firstName givenname given_name givenName]
48+
config[:attribute_statements][:lastname] ||= %w[last_name lastname lastName family_name familyName familyname]
49+
# rubocop:enable Layout/LineLength
50+
51+
config
52+
end
53+
54+
def find_attribute(attributes, attribute_statements)
55+
attribute_statements.each do |statement|
56+
return attributes[statement] unless attributes[statement].nil?
57+
end
58+
nil
59+
end
60+
end
61+
end
62+
end
63+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Code0
2+
module Identities
3+
module Provider
4+
class Oidc < BaseOauth
5+
def token_url: () -> String
6+
7+
def token_payload: (code: String) -> { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String }
8+
9+
def user_details_url: () -> String
10+
11+
def authorization_url: () -> String
12+
13+
def create_identity: (response: Net::HTTPResponse) -> Identity
14+
end
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)