2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2019-11-17 08:05:46 +07:00
|
|
|
"crypto/elliptic"
|
2019-04-25 04:52:08 +07:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-17 17:47:07 +07:00
|
|
|
"github.com/clems4ever/authelia/internal/middlewares"
|
|
|
|
"github.com/clems4ever/authelia/internal/session"
|
|
|
|
"github.com/clems4ever/authelia/internal/storage"
|
2019-04-25 04:52:08 +07:00
|
|
|
"github.com/tstranex/u2f"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SecondFactorU2FSignGet handler for initiating a signing request.
|
|
|
|
func SecondFactorU2FSignGet(ctx *middlewares.AutheliaCtx) {
|
|
|
|
userSession := ctx.GetSession()
|
|
|
|
|
|
|
|
appID := fmt.Sprintf("%s://%s", ctx.XForwardedProto(), ctx.XForwardedHost())
|
|
|
|
var trustedFacets = []string{appID}
|
|
|
|
challenge, err := u2f.NewChallenge(appID, trustedFacets)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Unable to create U2F challenge: %s", err), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-17 08:05:46 +07:00
|
|
|
keyHandleBytes, publicKeyBytes, err := ctx.Providers.StorageProvider.LoadU2FDeviceHandle(userSession.Username)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == storage.ErrNoU2FDeviceHandle {
|
|
|
|
ctx.Error(fmt.Errorf("No device handle found for user %s", userSession.Username), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(fmt.Errorf("Unable to retrieve U2F device handle: %s", err), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var registration u2f.Registration
|
2019-11-17 08:05:46 +07:00
|
|
|
registration.KeyHandle = keyHandleBytes
|
|
|
|
x, y := elliptic.Unmarshal(elliptic.P256(), publicKeyBytes)
|
|
|
|
registration.PubKey.Curve = elliptic.P256()
|
|
|
|
registration.PubKey.X = x
|
|
|
|
registration.PubKey.Y = y
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
// Save the challenge and registration for use in next request
|
2019-11-17 08:05:46 +07:00
|
|
|
userSession.U2FRegistration = &session.U2FRegistration{
|
|
|
|
KeyHandle: keyHandleBytes,
|
|
|
|
PublicKey: publicKeyBytes,
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
userSession.U2FChallenge = challenge
|
|
|
|
err = ctx.SaveSession(userSession)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Unable to save U2F challenge and registration in session: %s", err), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
signRequest := challenge.SignRequest([]u2f.Registration{registration})
|
|
|
|
err = ctx.SetJSONBody(signRequest)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Unable to set sign request in body: %s", err), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|