Skip to content

Commit 63d0d79

Browse files
committed
Close #117, store and check idp subject against supplied subject
1 parent 6a39ced commit 63d0d79

File tree

1 file changed

+26
-6
lines changed
  • internal/webserver/authenticators

1 file changed

+26
-6
lines changed

internal/webserver/authenticators/oidc.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
)
2525

2626
type issuer struct {
27-
Issuer string
27+
Issuer string
28+
Subject string
2829
}
2930

3031
type Oidc struct {
@@ -123,7 +124,8 @@ func (o *Oidc) RegistrationAPI(w http.ResponseWriter, r *http.Request) {
123124
log.Println(user.Username, clientTunnelIp, "registering with oidc")
124125

125126
value, _ := json.Marshal(issuer{
126-
Issuer: o.provider.Issuer(),
127+
Issuer: o.provider.Issuer(),
128+
Subject: "", // Empty is unconfigured waiting for first login
127129
})
128130

129131
err = data.SetUserMfa(user.Username, string(value), o.Type())
@@ -167,7 +169,7 @@ func (o *Oidc) AuthorisationAPI(w http.ResponseWriter, r *http.Request) {
167169
return
168170
}
169171

170-
deviceUsername := info.PreferredUsername
172+
suppliedUsername := info.PreferredUsername
171173

172174
if len(o.details.DeviceUsernameClaim) != 0 {
173175

@@ -178,7 +180,7 @@ func (o *Oidc) AuthorisationAPI(w http.ResponseWriter, r *http.Request) {
178180
return
179181
}
180182

181-
deviceUsername = deviceUsernameClaim
183+
suppliedUsername = deviceUsernameClaim
182184

183185
}
184186

@@ -207,8 +209,26 @@ func (o *Oidc) AuthorisationAPI(w http.ResponseWriter, r *http.Request) {
207209
return fmt.Errorf("stored issuer %q did not equal actual issuer: %q", issuerDetails.Issuer, rp.Issuer())
208210
}
209211

210-
if deviceUsername != username {
211-
log.Printf("Error logging in user, idP supplied device username (%q) does not equal expected username (%q)", deviceUsername, username)
212+
// On first OIDC login this will be unset
213+
if issuerDetails.Subject == "" {
214+
215+
issuerDetails.Subject = info.Subject
216+
217+
value, _ := json.Marshal(issuerDetails)
218+
219+
err = data.SetUserMfa(user.Username, string(value), o.Type())
220+
if err != nil {
221+
return fmt.Errorf("unable to set oidc subject: %s", err)
222+
}
223+
}
224+
225+
if issuerDetails.Subject != info.Subject {
226+
log.Printf("Error logging in user, idP supplied device username (%q) does not equal expected username (%q)", suppliedUsername, username)
227+
return fmt.Errorf("idp subject %q is not equal to subject %q associated with username %q", info.Subject, issuerDetails.Subject, username)
228+
}
229+
230+
if suppliedUsername != username {
231+
log.Printf("Error logging in user, idP supplied username (%q) does not equal username (%q) associated with device", suppliedUsername, username)
212232
return errors.New("user is not associated with device")
213233
}
214234

0 commit comments

Comments
 (0)