2019-04-24 21:52:08 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2019-11-17 01:05:46 +00:00
|
|
|
"crypto/elliptic"
|
2019-04-24 21:52:08 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
"github.com/tstranex/u2f"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
|
|
|
"github.com/authelia/authelia/v4/internal/storage"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecondFactorU2FSignGet handler for initiating a signing request.
|
|
|
|
func SecondFactorU2FSignGet(ctx *middlewares.AutheliaCtx) {
|
2020-02-13 02:12:37 +00:00
|
|
|
if ctx.XForwardedProto() == nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(errMissingXForwardedProto, messageMFAValidationFailed)
|
2020-02-13 02:12:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.XForwardedHost() == nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(errMissingXForwardedHost, messageMFAValidationFailed)
|
2020-02-13 02:12:37 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
appID := fmt.Sprintf("%s://%s", ctx.XForwardedProto(), ctx.XForwardedHost())
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
var trustedFacets = []string{appID}
|
|
|
|
challenge, err := u2f.NewChallenge(appID, trustedFacets)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-17 05:53:40 +00:00
|
|
|
handleAuthenticationUnauthorized(ctx, fmt.Errorf("unable to create U2F challenge: %s", err), messageMFAValidationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-13 02:12:37 +00:00
|
|
|
userSession := ctx.GetSession()
|
2021-11-23 09:45:38 +00:00
|
|
|
device, err := ctx.Providers.StorageProvider.LoadU2FDevice(ctx, userSession.Username)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == storage.ErrNoU2FDeviceHandle {
|
2021-09-17 05:53:40 +00:00
|
|
|
handleAuthenticationUnauthorized(ctx, fmt.Errorf("no device handle found for user %s", userSession.Username), messageMFAValidationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-09-17 05:53:40 +00:00
|
|
|
handleAuthenticationUnauthorized(ctx, fmt.Errorf("unable to retrieve U2F device handle: %s", err), messageMFAValidationFailed)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var registration u2f.Registration
|
2021-11-23 09:45:38 +00:00
|
|
|
registration.KeyHandle = device.KeyHandle
|
|
|
|
x, y := elliptic.Unmarshal(elliptic.P256(), device.PublicKey)
|
2019-11-17 01:05:46 +00:00
|
|
|
registration.PubKey.Curve = elliptic.P256()
|
|
|
|
registration.PubKey.X = x
|
|
|
|
registration.PubKey.Y = y
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
// Save the challenge and registration for use in next request
|
2019-11-17 01:05:46 +00:00
|
|
|
userSession.U2FRegistration = &session.U2FRegistration{
|
2021-11-23 09:45:38 +00:00
|
|
|
KeyHandle: device.KeyHandle,
|
|
|
|
PublicKey: device.PublicKey,
|
2019-11-17 01:05:46 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
userSession.U2FChallenge = challenge
|
|
|
|
err = ctx.SaveSession(userSession)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-17 05:53:40 +00:00
|
|
|
handleAuthenticationUnauthorized(ctx, fmt.Errorf("unable to save U2F challenge and registration in session: %s", err), messageMFAValidationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
signRequest := challenge.SignRequest([]u2f.Registration{registration})
|
|
|
|
err = ctx.SetJSONBody(signRequest)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-17 05:53:40 +00:00
|
|
|
handleAuthenticationUnauthorized(ctx, fmt.Errorf("unable to set sign request in body: %s", err), messageMFAValidationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|