Skip to content

Commit bb52436

Browse files
committed
Respect RegistrationOIDC.RequireInvite
This previously setting had no effect when clicking 'Sign in with $PROVIDER' on the home page.
1 parent 1c53e8c commit bb52436

File tree

3 files changed

+85
-84
lines changed

3 files changed

+85
-84
lines changed

front.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,11 @@ func (app *App) getIDTokenCookie(c *echo.Context) (*OIDCProvider, string, oidc.I
560560
func FrontCompleteRegistration(app *App) func(c echo.Context) error {
561561
type completeRegistrationContext struct {
562562
baseContext
563-
User *User
564-
InviteCode string
565-
AnyUnmigratedUsers bool
566-
AllowChoosingPlayerName bool
567-
PreferredPlayerName string
563+
User *User
564+
InviteCode string
565+
OIDCProvider *OIDCProvider
566+
AnyUnmigratedUsers bool
567+
PreferredPlayerName string
568568
}
569569

570570
returnURL := Unwrap(url.JoinPath(app.FrontEndURL, "web/registration"))
@@ -600,12 +600,12 @@ func FrontCompleteRegistration(app *App) func(c echo.Context) error {
600600
}
601601

602602
return c.Render(http.StatusOK, "complete-registration", completeRegistrationContext{
603-
baseContext: app.NewBaseContext(&c),
604-
User: user,
605-
InviteCode: inviteCode,
606-
PreferredPlayerName: preferredPlayerName,
607-
AllowChoosingPlayerName: provider.Config.AllowChoosingPlayerName,
608-
AnyUnmigratedUsers: anyUnmigratedUsers,
603+
baseContext: app.NewBaseContext(&c),
604+
User: user,
605+
InviteCode: inviteCode,
606+
PreferredPlayerName: preferredPlayerName,
607+
OIDCProvider: provider,
608+
AnyUnmigratedUsers: anyUnmigratedUsers,
609609
})
610610
})
611611
}

user.go

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ func (app *App) CreateUser(
9595
}
9696
}
9797

98+
var invite mo.Option[Invite]
99+
if inviteCode != nil {
100+
var inviteStruct Invite
101+
result := app.DB.First(&inviteStruct, "code = ?", *inviteCode)
102+
if result.Error != nil {
103+
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
104+
return User{}, InviteNotFoundError
105+
} else {
106+
return User{}, result.Error
107+
}
108+
} else {
109+
invite = mo.Some(inviteStruct)
110+
}
111+
}
112+
98113
oidcIdentities := make([]UserOIDCIdentity, 0, len(oidcIdentitySpecs.Value))
99114
for _, oidcIdentitySpec := range oidcIdentitySpecs.Value {
100115
provider, ok := app.OIDCProvidersByIssuer[oidcIdentitySpec.Issuer]
@@ -104,6 +119,9 @@ func (app *App) CreateUser(
104119
if oidcIdentitySpec.Subject == "" {
105120
return User{}, NewBadRequestUserError("OIDC subject for provider %s can't be blank.", provider.Config.Issuer)
106121
}
122+
if !callerIsAdmin && invite.IsAbsent() && provider.Config.RequireInvite {
123+
return User{}, InviteMissingError
124+
}
107125
oidcIdentities = append(oidcIdentities, UserOIDCIdentity{
108126
UserUUID: userUUID,
109127
Issuer: provider.Config.Issuer,
@@ -125,37 +143,15 @@ func (app *App) CreateUser(
125143
return User{}, NewBadRequestUserError("Invalid preferred language.")
126144
}
127145

128-
getInvite := func(requireInvite bool) (*Invite, error) {
129-
var invite Invite
130-
if inviteCode == nil {
131-
if requireInvite && !callerIsAdmin {
132-
return nil, InviteMissingError
133-
}
134-
return nil, nil
135-
} else {
136-
result := app.DB.First(&invite, "code = ?", *inviteCode)
137-
if result.Error != nil {
138-
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
139-
return nil, InviteNotFoundError
140-
}
141-
return nil, result.Error
142-
}
143-
return &invite, nil
144-
}
145-
}
146-
147-
var invite *Invite
148146
var playerUUID string
149147
if existingPlayer {
150148
// Existing player registration
151149
if !app.Config.RegistrationExistingPlayer.Allow && !callerIsAdmin {
152150
return User{}, NewBadRequestUserError("Registration from an existing player is not allowed.")
153151
}
154152

155-
var err error
156-
invite, err = getInvite(app.Config.RegistrationExistingPlayer.RequireInvite)
157-
if err != nil {
158-
return User{}, err
153+
if !callerIsAdmin && invite.IsAbsent() && app.Config.RegistrationExistingPlayer.RequireInvite {
154+
return User{}, InviteMissingError
159155
}
160156

161157
if err := app.ValidatePlayerName(*playerName); err != nil {
@@ -182,13 +178,12 @@ func (app *App) CreateUser(
182178
return User{}, NewBadRequestUserError("Registration without some existing player is not allowed.")
183179
}
184180

185-
var err error
186-
invite, err = getInvite(app.Config.RegistrationNewPlayer.RequireInvite)
187-
if err != nil {
188-
return User{}, err
181+
if !callerIsAdmin && invite.IsAbsent() && app.Config.RegistrationNewPlayer.RequireInvite {
182+
return User{}, InviteMissingError
189183
}
190184

191185
if chosenUUID == nil {
186+
var err error
192187
playerUUID, err = app.NewPlayerUUID(*playerName)
193188
if err != nil {
194189
return User{}, err
@@ -332,8 +327,8 @@ func (app *App) CreateUser(
332327
}
333328
}
334329

335-
if invite != nil {
336-
if err := tx.Delete(invite).Error; err != nil {
330+
if i, ok := invite.Get(); ok {
331+
if err := tx.Delete(i).Error; err != nil {
337332
return User{}, err
338333
}
339334
}

view/complete-registration.tmpl

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{{ end }}
1717
<h3>{{ call .T "Migrate an existing user" }}</h3>
1818

19-
<p>{{ call .T "You can link this identity provider to an existing %s account." }} <span class="warning-message">{{ call .T "If you do so, you will no longer be able to log in using your %s password. You'll need to use your Minecraft Token to log in to Minecraft launchers." }}</span></p>
19+
<p>{{ call .T "You can link this identity provider to an existing %s account." .App.Config.ApplicationName }} <span class="warning-message">{{ call .T "If you do so, you will no longer be able to log in using your %s password. You'll need to use your Minecraft Token to log in to Minecraft launchers." .App.Config.ApplicationName }}</span></p>
2020

2121
<form action="{{ .App.FrontEndURL }}/web/oidc-migrate" method="post">
2222
<input type="text" name="username" placeholder="{{ call .T "Username" }}" required />
@@ -29,7 +29,7 @@
2929
/>
3030
<input type="submit" value="{{ call .T "Link account" }}" />
3131
</form>
32-
{{ $dividerNeeded := true }}
32+
{{ $dividerNeeded = true }}
3333
{{ end }}
3434

3535
<!-- CreateNewPlayer -->
@@ -39,46 +39,52 @@
3939
{{ $dividerNeeded = false }}
4040
{{ end }}
4141
<h3>{{ call .T "Create a player" }}</h3>
42-
<p>{{ call .T "Complete registration by creating a new player:" }}</p>
43-
<form action="{{ .App.FrontEndURL }}/web/register" method="post">
44-
<input
45-
required
46-
type="text"
47-
name="playerName"
48-
placeholder="{{ call .T "Player name" }}"
49-
maxlength="{{ .App.Constants.MaxUsernameLength }}"
50-
value="{{ .PreferredPlayerName }}"
51-
{{ if not .AllowChoosingPlayerName }}
52-
title="{{ call .T "Choosing a player name is not allowed." }}"
53-
disabled
42+
{{ if and .OIDCProvider.Config.RequireInvite (not .InviteCode) }}
43+
<p>{{ call .T "Registration with %s requires an invite." .OIDCProvider.Config.Name }}</p>
44+
{{ else if and .App.Config.RegistrationExistingPlayer.RequireInvite (not .InviteCode) }}
45+
<p>{{ call .T "Registration as a new player is invite-only." }}</p>
46+
{{ else }}
47+
<p>{{ call .T "Complete registration by creating a new player:" }}</p>
48+
<form action="{{ .App.FrontEndURL }}/web/register" method="post">
49+
<input
50+
required
51+
type="text"
52+
name="playerName"
53+
placeholder="{{ call .T "Player name" }}"
54+
maxlength="{{ .App.Constants.MaxUsernameLength }}"
55+
value="{{ .PreferredPlayerName }}"
56+
{{ if not .OIDCProvider.Config.AllowChoosingPlayerName }}
57+
title="{{ call .T "Choosing a player name is not allowed." }}"
58+
disabled
59+
{{ end }}
60+
/>
61+
<input
62+
hidden
63+
type="checkbox"
64+
name="useIdToken"
65+
checked
66+
/>
67+
{{ if .App.Config.CreateNewPlayer.AllowChoosingUUID }}
68+
<p>
69+
<input
70+
class="long"
71+
type="text"
72+
name="uuid"
73+
placeholder="{{ if eq .App.Config.PlayerUUIDGeneration "offline" }}{{ call .T "Player UUID (leave blank for offline UUID)" }}{{ else }}{{ call .T "Player UUID (leave blank for random)" }}{{ end }}"
74+
pattern="^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$"
75+
/>
76+
</p>
77+
{{ end }}
78+
<input type="text" name="inviteCode" value="{{ .InviteCode }}" hidden />
79+
<input hidden name="returnUrl" value="{{ .URL }}" />
80+
{{ if .InviteCode }}
81+
<p><em>{{ call .T "Using invite code %s" .InviteCode }}</em></p>
5482
{{ end }}
55-
/>
56-
<input
57-
hidden
58-
type="checkbox"
59-
name="useIdToken"
60-
checked
61-
/>
62-
{{ if .App.Config.CreateNewPlayer.AllowChoosingUUID }}
6383
<p>
64-
<input
65-
class="long"
66-
type="text"
67-
name="uuid"
68-
placeholder="{{ if eq .App.Config.PlayerUUIDGeneration "offline" }}{{ call .T "Player UUID (leave blank for offline UUID)" }}{{ else }}{{ call .T "Player UUID (leave blank for random)" }}{{ end }}"
69-
pattern="^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$"
70-
/>
84+
<input type="submit" value="{{ call .T "Register" }}"/>
7185
</p>
72-
{{ end }}
73-
<input type="text" name="inviteCode" value="{{ .InviteCode }}" hidden />
74-
<input hidden name="returnUrl" value="{{ .URL }}" />
75-
{{ if .InviteCode }}
76-
<p><em>{{ call .T "Using invite code %s" .InviteCode }}</em></p>
77-
{{ end }}
78-
<p>
79-
<input type="submit" value="{{ call .T "Register" }}"/>
80-
</p>
81-
</form>
86+
</form>
87+
{{ end }}
8288
{{ $dividerNeeded = true }}
8389
{{ end }}
8490

@@ -89,9 +95,9 @@
8995
{{ $dividerNeeded = false }}
9096
{{ end }}
9197
<h3>{{ call .T "Register from an existing Minecraft player" }}</h3>
92-
{{ if and .App.Config.RegistrationExistingPlayer.RequireInvite (not
93-
.InviteCode)
94-
}}
98+
{{ if and .OIDCProvider.Config.RequireInvite (not .InviteCode) }}
99+
<p>{{ call .T "Registration with %s requires an invite." .OIDCProvider.Config.Name }}</p>
100+
{{ else if and .App.Config.RegistrationExistingPlayer.RequireInvite (not .InviteCode) }}
95101
<p>{{ call .T "Registration as an existing player is invite-only." }}</p>
96102
{{ else }}
97103
{{ if .App.Config.ImportExistingPlayer.RequireSkinVerification }}
@@ -111,7 +117,7 @@
111117
name="playerName"
112118
placeholder="{{ call .T "%s player name" .App.Config.ImportExistingPlayer.Nickname }}"
113119
maxlength="{{ .App.Constants.MaxUsernameLength }}"
114-
{{ if not .AllowChoosingPlayerName }}
120+
{{ if not .OIDCProvider.Config.AllowChoosingPlayerName }}
115121
value="{{ .PreferredPlayerName }}"
116122
title="{{ call .T "Choosing a player name is not allowed." }}"
117123
disabled

0 commit comments

Comments
 (0)