Skip to content

Commit 1932b66

Browse files
committed
Add OpenID Connect login support
1 parent eba42e2 commit 1932b66

File tree

12 files changed

+149
-13
lines changed

12 files changed

+149
-13
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ POSTGRES_USER=postgres
5353
# This is the domain that your Maybe instance will be hosted at. It is used to generate links in emails and other places.
5454
APP_DOMAIN=
5555

56+
# OpenID Connect configuration
57+
OIDC_ISSUER=
58+
OIDC_CLIENT_ID=
59+
OIDC_CLIENT_SECRET=
60+
OIDC_REDIRECT_URI=http://localhost:3000/auth/openid_connect/callback
61+
5662
# Disable enforcing SSL connections
5763
# DISABLE_SSL=true
5864

.env.local.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ SELF_HOSTED=false
33

44
# Enable Twelve market data (careful, this will use your API credits)
55
TWELVE_DATA_API_KEY=yourapikeyhere
6+
7+
# OpenID Connect for development
8+
OIDC_ISSUER=https://example.com
9+
OIDC_CLIENT_ID=
10+
OIDC_CLIENT_SECRET=
11+
OIDC_REDIRECT_URI=http://localhost:3000/auth/openid_connect/callback

.env.test.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
SELF_HOSTED=false
22

3+
# OpenID Connect for tests
4+
OIDC_ISSUER=
5+
OIDC_CLIENT_ID=
6+
OIDC_CLIENT_SECRET=
7+
OIDC_REDIRECT_URI=http://localhost:3000/auth/openid_connect/callback
8+
39
# ================
410
# Data Providers
511
# ---------------------------------------------------------------------------------

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ gem "rqrcode", "~> 3.0"
7474
gem "activerecord-import"
7575
gem "rubyzip", "~> 2.3"
7676

77+
# OpenID Connect authentication
78+
gem "omniauth", "~> 2.1"
79+
gem "omniauth-rails_csrf_protection"
80+
gem "omniauth_openid_connect"
81+
7782
# State machines
7883
gem "aasm"
7984
gem "after_commit_everywhere", "~> 1.0"

Gemfile.lock

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ GEM
8585
tzinfo (~> 2.0, >= 2.0.5)
8686
addressable (2.8.7)
8787
public_suffix (>= 2.0.2, < 7.0)
88+
aes_key_wrap (1.1.0)
8889
after_commit_everywhere (1.6.0)
8990
activerecord (>= 4.2)
9091
activesupport
9192
ast (2.4.3)
93+
attr_required (1.0.2)
9294
aws-eventstream (1.4.0)
9395
aws-partitions (1.1113.0)
9496
aws-sdk-core (3.225.1)
@@ -119,6 +121,7 @@ GEM
119121
parser (>= 2.4)
120122
smart_properties
121123
bigdecimal (3.2.2)
124+
bindata (2.5.1)
122125
bindex (0.8.1)
123126
bootsnap (1.18.6)
124127
msgpack (~> 1.2)
@@ -182,6 +185,8 @@ GEM
182185
dotenv (= 3.1.8)
183186
railties (>= 6.1)
184187
drb (2.2.3)
188+
email_validator (2.2.4)
189+
activemodel
185190
erb (5.0.1)
186191
erb_lint (0.9.0)
187192
activesupport
@@ -200,6 +205,8 @@ GEM
200205
faraday-net_http (>= 2.0, < 3.5)
201206
json
202207
logger
208+
faraday-follow_redirects (0.3.0)
209+
faraday (>= 1, < 3)
203210
faraday-multipart (1.1.1)
204211
multipart-post (~> 2.0)
205212
faraday-net_http (3.4.1)
@@ -224,6 +231,7 @@ GEM
224231
globalid (1.2.1)
225232
activesupport (>= 6.1)
226233
hashdiff (1.2.0)
234+
hashie (5.0.0)
227235
heapy (0.2.0)
228236
thor
229237
highline (3.1.2)
@@ -272,6 +280,13 @@ GEM
272280
activesupport (>= 5.0.0)
273281
jmespath (1.6.2)
274282
json (2.12.2)
283+
json-jwt (1.16.7)
284+
activesupport (>= 4.2)
285+
aes_key_wrap
286+
base64
287+
bindata
288+
faraday (~> 2.0)
289+
faraday-follow_redirects
275290
jwt (2.10.2)
276291
base64
277292
language_server-protocol (3.17.0.5)
@@ -363,6 +378,29 @@ GEM
363378
octokit (10.0.0)
364379
faraday (>= 1, < 3)
365380
sawyer (~> 0.9)
381+
omniauth (2.1.3)
382+
hashie (>= 3.4.6)
383+
rack (>= 2.2.3)
384+
rack-protection
385+
omniauth-rails_csrf_protection (1.0.2)
386+
actionpack (>= 4.2)
387+
omniauth (~> 2.0)
388+
omniauth_openid_connect (0.8.0)
389+
omniauth (>= 1.9, < 3)
390+
openid_connect (~> 2.2)
391+
openid_connect (2.3.1)
392+
activemodel
393+
attr_required (>= 1.0.0)
394+
email_validator
395+
faraday (~> 2.0)
396+
faraday-follow_redirects
397+
json-jwt (>= 1.16)
398+
mail
399+
rack-oauth2 (~> 2.2)
400+
swd (~> 2.0)
401+
tzinfo
402+
validate_url
403+
webfinger (~> 2.0)
366404
ostruct (0.6.2)
367405
pagy (9.3.5)
368406
parallel (1.27.0)
@@ -398,6 +436,17 @@ GEM
398436
rack (>= 1.0, < 4)
399437
rack-mini-profiler (4.0.0)
400438
rack (>= 1.2.0)
439+
rack-oauth2 (2.2.1)
440+
activesupport
441+
attr_required
442+
faraday (~> 2.0)
443+
faraday-follow_redirects
444+
json-jwt (>= 1.11.0)
445+
rack (>= 2.1.0)
446+
rack-protection (4.1.1)
447+
base64 (>= 0.1.0)
448+
logger (>= 1.6.0)
449+
rack (>= 3.0.0, < 4)
401450
rack-session (2.1.1)
402451
base64 (>= 0.1.0)
403452
rack (>= 3.0.0)
@@ -556,6 +605,11 @@ GEM
556605
railties (>= 6.0.0)
557606
stringio (3.1.7)
558607
stripe (15.3.0)
608+
swd (2.0.3)
609+
activesupport (>= 3)
610+
attr_required (>= 0.0.5)
611+
faraday (~> 2.0)
612+
faraday-follow_redirects
559613
tailwindcss-rails (4.2.3)
560614
railties (>= 7.0.0)
561615
tailwindcss-ruby (~> 4.0)
@@ -582,6 +636,9 @@ GEM
582636
unicode-emoji (4.0.4)
583637
uri (1.0.3)
584638
useragent (0.16.11)
639+
validate_url (1.0.15)
640+
activemodel (>= 3.0.0)
641+
public_suffix
585642
vcr (6.3.1)
586643
base64
587644
vernier (1.8.0)
@@ -594,6 +651,10 @@ GEM
594651
activemodel (>= 6.0.0)
595652
bindex (>= 0.4.0)
596653
railties (>= 6.0.0)
654+
webfinger (2.1.3)
655+
activesupport
656+
faraday (~> 2.0)
657+
faraday-follow_redirects
597658
webmock (3.25.1)
598659
addressable (>= 2.8.0)
599660
crack (>= 0.3.2)
@@ -655,6 +716,9 @@ DEPENDENCIES
655716
lucide-rails!
656717
mocha
657718
octokit
719+
omniauth (~> 2.1)
720+
omniauth-rails_csrf_protection
721+
omniauth_openid_connect
658722
ostruct
659723
pagy
660724
pg (~> 1.5)

app/controllers/sessions_controller.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class SessionsController < ApplicationController
22
before_action :set_session, only: :destroy
3-
skip_authentication only: %i[new create]
3+
skip_authentication only: %i[new create openid_connect failure]
44

55
layout "auth"
66

@@ -27,6 +27,20 @@ def destroy
2727
redirect_to new_session_path, notice: t(".logout_successful")
2828
end
2929

30+
def openid_connect
31+
auth = request.env["omniauth.auth"]
32+
if auth && (user = User.find_by(email: auth.info.email))
33+
@session = create_session_for(user)
34+
redirect_to root_path
35+
else
36+
redirect_to new_session_path, alert: t(".failed")
37+
end
38+
end
39+
40+
def failure
41+
redirect_to new_session_path, alert: t(".failed")
42+
end
43+
3044
private
3145
def set_session
3246
@session = Current.user.sessions.find(params[:id])

app/views/sessions/new.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
<div class="mt-6 text-center">
1414
<%= link_to t(".forgot_password"), new_password_reset_path, class: "font-medium text-sm text-primary hover:underline transition" %>
1515
</div>
16+
17+
<div class="mt-6 text-center">
18+
<%= link_to t(".openid_connect"), "/auth/openid_connect", class: "font-medium text-sm text-primary hover:underline transition" %>
19+
</div>

config/initializers/omniauth.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require "omniauth/rails_csrf_protection"
4+
5+
if ENV["OIDC_ISSUER"].present?
6+
Rails.application.config.middleware.use OmniAuth::Builder do
7+
provider :openid_connect,
8+
name: :openid_connect,
9+
scope: %i[openid email profile],
10+
response_type: :code,
11+
issuer: ENV["OIDC_ISSUER"],
12+
discovery: true,
13+
pkce: true,
14+
client_options: {
15+
identifier: ENV["OIDC_CLIENT_ID"],
16+
secret: ENV["OIDC_CLIENT_SECRET"],
17+
redirect_uri: ENV["OIDC_REDIRECT_URI"]
18+
}
19+
end
20+
end

config/locales/views/sessions/en.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ en:
55
invalid_credentials: Invalid email or password.
66
destroy:
77
logout_successful: You have signed out successfully.
8+
openid_connect:
9+
failed: Could not authenticate via OpenID Connect.
810
new:
911
email: Email address
1012
email_placeholder: [email protected]
1113
forgot_password: Forgot your password?
1214
password: Password
1315
submit: Log in
1416
title: Sign in to your account
15-
password_placeholder: Enter your password
17+
password_placeholder: Enter your password
18+
openid_connect: Sign in with OpenID Connect

config/locales/views/sessions/nb.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ nb:
33
sessions:
44
create:
55
invalid_credentials: Ugyldig e-post eller passord.
6-
destroy:
7-
logout_successful: Du har blitt logget ut.
8-
new:
9-
email: E-postadresse
10-
email_placeholder: [email protected]
11-
forgot_password: Glemt passordet ditt?
12-
password: Passord
13-
submit: Logg inn
14-
title: Logg inn på kontoen din
15-
password_placeholder: Angi passordet ditt
6+
destroy:
7+
logout_successful: Du har blitt logget ut.
8+
openid_connect:
9+
failed: Kunne ikke autentisere via OpenID Connect.
10+
new:
11+
email: E-postadresse
12+
email_placeholder: [email protected]
13+
forgot_password: Glemt passordet ditt?
14+
password: Passord
15+
submit: Logg inn
16+
title: Logg inn på kontoen din
17+
password_placeholder: Angi passordet ditt
18+
openid_connect: Logg inn med OpenID Connect

0 commit comments

Comments
 (0)