Skip to content

Commit f71fccf

Browse files
authored
Merge pull request #12 from code0-tech/providers/oidc
implement oidc as a new provider
2 parents fc2d248 + a38fc2b commit f71fccf

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ OAuth:
1010
- Microsoft
1111
- Github
1212
- Gitlab
13+
- OIDC / oAuth2
1314
- SAML
1415

1516
## Installation

lib/code0/identities.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require_relative "identities/provider/google"
1212
require_relative "identities/provider/discord"
1313
require_relative "identities/provider/github"
14+
require_relative "identities/provider/oidc"
1415
require_relative "identities/provider/saml"
1516

1617
module Code0

lib/code0/identities/provider/oidc.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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[:authorization_url]
25+
.gsub("{client_id}", config[:client_id])
26+
.gsub("{redirect_uri}", config[:redirect_uri])
27+
end
28+
29+
def create_identity(response, *)
30+
body = response.parsed_response
31+
32+
Identity.new(config[:provider_name],
33+
find_attribute(body, config[:attribute_statements][:identifier]),
34+
find_attribute(body, config[:attribute_statements][:username]),
35+
find_attribute(body, config[:attribute_statements][:email]),
36+
find_attribute(body, config[:attribute_statements][:firstname]),
37+
find_attribute(body, config[:attribute_statements][:lastname]))
38+
end
39+
40+
def config
41+
config = super
42+
43+
# rubocop:disable Layout/LineLength
44+
config[:provider_name] ||= :oidc
45+
config[:attribute_statements] ||= {}
46+
config[:attribute_statements][:identifier] ||= %w[sub id identifier]
47+
config[:attribute_statements][:username] ||= %w[username name login]
48+
config[:attribute_statements][:email] ||= %w[email mail]
49+
config[:attribute_statements][:firstname] ||= %w[first_name firstname firstName givenname given_name givenName]
50+
config[:attribute_statements][:lastname] ||= %w[last_name lastname lastName family_name familyName familyname]
51+
# rubocop:enable Layout/LineLength
52+
53+
config
54+
end
55+
56+
def find_attribute(attributes, attribute_statements)
57+
attribute_statements.each do |statement|
58+
return attributes[statement] unless attributes[statement].nil?
59+
end
60+
nil
61+
end
62+
end
63+
end
64+
end
65+
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)