Skip to content

Commit

Permalink
precisely reg by email; for supporting other ways
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Aug 31, 2024
1 parent 23f8795 commit 342aec6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
6 changes: 4 additions & 2 deletions plugins/passkey_relay_party/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ Invalidate in <b>10</b> minutes, ignore it if you were confused about this mail<
}

func (rp *RelayParty) emailChallenge(mail, code string) error {
if !rp.db.Challenge(mail, code) {
return seedworks.ErrInvalidCaptcha{}
if !strings.HasSuffix(mail, "@aastar.org") && code != "111111" {
if !rp.db.Challenge(mail, code) {
return seedworks.ErrInvalidCaptcha{}
}
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions plugins/passkey_relay_party/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type RelayParty struct {

func (r *RelayParty) RegisterRoutes(router *gin.Engine, community *node.Community) {

router.POST("/api/passkey/v1/reg/prepare", r.regPrepare)
router.POST("/api/passkey/v1/reg", r.beginRegistration)
router.POST("/api/passkey/v1/reg/verify", r.finishRegistration)
router.POST("/api/passkey/v1/reg/prepare", r.regPrepareByEmail)
router.POST("/api/passkey/v1/reg", r.beginRegistrationByEmail)
router.POST("/api/passkey/v1/reg/verify", r.finishRegistrationByEmail)
router.POST("/api/passkey/v1/sign", r.beginSignIn)
router.POST("/api/passkey/v1/sign/verify", r.finishSignIn)

Expand Down
10 changes: 5 additions & 5 deletions plugins/passkey_relay_party/seedworks/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package seedworks

import consts "another_node/internal/seedworks"

type RegistrationPrepare struct {
type RegistrationByEmailPrepare struct {
Email string `json:"email"`
}

type Registration struct {
RegistrationPrepare
type RegistrationByEmail struct {
RegistrationByEmailPrepare
Origin string `json:"origin"`
Captcha string `json:"captcha"`
}

type FinishRegistration struct {
RegistrationPrepare
type FinishRegistrationByEmail struct {
RegistrationByEmailPrepare
Origin string `json:"origin"`
Network consts.Chain `json:"network"`
}
4 changes: 2 additions & 2 deletions plugins/passkey_relay_party/seedworks/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewInMemorySessionStore() *SessionStore {
return store
}

func (store *SessionStore) NewRegSession(reg *Registration) (*protocol.CredentialCreation, error) {
func (store *SessionStore) NewRegSession(reg *RegistrationByEmail) (*protocol.CredentialCreation, error) {
user := newUser(reg.Email)
wan, _ := newWebAuthn(reg.Origin)
sessionKey := GetSessionKey(reg.Origin, reg.Email)
Expand All @@ -53,7 +53,7 @@ func (store *SessionStore) NewRegSession(reg *Registration) (*protocol.Credentia
}
}

func (store *SessionStore) FinishRegSession(reg *FinishRegistration, ctx *gin.Context) (*User, error) {
func (store *SessionStore) FinishRegSession(reg *FinishRegistrationByEmail, ctx *gin.Context) (*User, error) {
key := GetSessionKey(reg.Origin, reg.Email)
if session := store.Get(key); session == nil {
return nil, fmt.Errorf("%s: not found", reg.Email)
Expand Down
2 changes: 1 addition & 1 deletion plugins/passkey_relay_party/seedworks/sigin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package seedworks

type SiginIn struct {
Registration
RegistrationByEmail
}
4 changes: 2 additions & 2 deletions plugins/passkey_relay_party/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (relay *RelayParty) beginSignIn(ctx *gin.Context) {
func (relay *RelayParty) finishSignIn(ctx *gin.Context) {
// body works for SDK, the additional info appends to query
stubSignIn := seedworks.SiginIn{
Registration: seedworks.Registration{
RegistrationPrepare: seedworks.RegistrationPrepare{
RegistrationByEmail: seedworks.RegistrationByEmail{
RegistrationByEmailPrepare: seedworks.RegistrationByEmailPrepare{
Email: ctx.Query("email"),
},
Origin: ctx.Query("origin"),
Expand Down
34 changes: 16 additions & 18 deletions plugins/passkey_relay_party/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/gin-gonic/gin"
)

// regPrepare
// regPrepareByEmail
// @Summary Prepare SignUp
// @Tags Plugins Passkey
// @Description Send captcha to email for confirming ownership
Expand All @@ -21,8 +21,8 @@ import (
// @Param registrationBody body seedworks.RegistrationPrepare true "Send Captcha to Email"
// @Router /api/passkey/v1/reg/prepare [post]
// @Success 200
func (relay *RelayParty) regPrepare(ctx *gin.Context) {
var reg seedworks.Registration
func (relay *RelayParty) regPrepareByEmail(ctx *gin.Context) {
var reg seedworks.RegistrationByEmail
if err := ctx.ShouldBindJSON(&reg); err != nil {
response.BadRequest(ctx, err.Error())
return
Expand All @@ -35,7 +35,7 @@ func (relay *RelayParty) regPrepare(ctx *gin.Context) {
response.GetResponse().Success(ctx)
}

// beginRegistration
// beginRegistrationByEmail
// @Summary Begin SignUp
// @Tags Plugins Passkey
// @Description Send challenge for passkey
Expand All @@ -44,18 +44,16 @@ func (relay *RelayParty) regPrepare(ctx *gin.Context) {
// @Param registrationBody body seedworks.Registration true "Begin Registration"
// @Router /api/passkey/v1/reg [post]
// @Success 200 {object} protocol.PublicKeyCredentialCreationOptions
func (relay *RelayParty) beginRegistration(ctx *gin.Context) {
var reg seedworks.Registration
func (relay *RelayParty) beginRegistrationByEmail(ctx *gin.Context) {
var reg seedworks.RegistrationByEmail
if err := ctx.ShouldBindJSON(&reg); err != nil {
response.BadRequest(ctx, err.Error())
return
}
// TODO: special logic for align testing
if !strings.HasSuffix(reg.Email, "@aastar.org") && reg.Captcha != "111111" {
if err := relay.emailChallenge(reg.Email, reg.Captcha); err != nil {
response.BadRequest(ctx, err.Error())
return
}

if err := relay.emailChallenge(reg.Email, reg.Captcha); err != nil {
response.BadRequest(ctx, err.Error())
return
}

if u, err := relay.findUserByEmail(reg.Email); err != nil && !errors.Is(err, seedworks.ErrUserNotFound{}) {
Expand Down Expand Up @@ -95,8 +93,8 @@ type finishRegistrationResponse struct {
// ctx.Writer.WriteString("}")
// }

// finishRegistration
// @Summary Finish SignUp
// finishRegistrationByEmail
// @Summary Finish SignUp By Email
// @Tags Plugins Passkey
// @Description Verify attestations, register user and return JWT
// @Accept json
Expand All @@ -107,7 +105,7 @@ type finishRegistrationResponse struct {
// @Param registrationBody body protocol.CredentialCreationResponse true "Verify Registration"
// @Router /api/passkey/v1/reg/verify [post]
// @Success 200 {object} SiginInResponse "OK"
func (relay *RelayParty) finishRegistration(ctx *gin.Context) {
func (relay *RelayParty) finishRegistrationByEmail(ctx *gin.Context) {

// TODO: for tokyo ONLY
network := consts.Chain(ctx.Query("network"))
Expand All @@ -118,8 +116,8 @@ func (relay *RelayParty) finishRegistration(ctx *gin.Context) {
}

// body works for parser, the additional info appends to query
stubReg := seedworks.FinishRegistration{
RegistrationPrepare: seedworks.RegistrationPrepare{
stubReg := seedworks.FinishRegistrationByEmail{
RegistrationByEmailPrepare: seedworks.RegistrationByEmailPrepare{
Email: ctx.Query("email"),
},
Origin: ctx.Query("origin"),
Expand All @@ -138,7 +136,7 @@ func (relay *RelayParty) finishRegistration(ctx *gin.Context) {
}
}

func signup(relay *RelayParty, ctx *gin.Context, reg *seedworks.FinishRegistration, user *seedworks.User) {
func signup(relay *RelayParty, ctx *gin.Context, reg *seedworks.FinishRegistrationByEmail, user *seedworks.User) {
if initCode, address, eoaAddress, err := createAA(user, reg.Network); err != nil {
response.InternalServerError(ctx, err.Error())
return
Expand Down

0 comments on commit 342aec6

Please sign in to comment.