add email

This commit is contained in:
Denis Mishin 2023-04-28 10:22:56 -04:00
parent 1530e8f9de
commit 60e229871d

View file

@ -36,6 +36,8 @@ type AuthEvent struct {
PubKey string PubKey string
// UID is the IdP user ID of the user // UID is the IdP user ID of the user
UID *string UID *string
// Email is the email of the user
Email *string
// Domain is the domain of the request (for sign in complete events) // Domain is the domain of the request (for sign in complete events)
Domain *string Domain *string
} }
@ -44,6 +46,10 @@ type AuthEvent struct {
type AuthEventFn func(context.Context, AuthEvent) type AuthEventFn func(context.Context, AuthEvent)
func (a *Authenticate) logAuthenticateEvent(r *http.Request, profile *identity.Profile) { func (a *Authenticate) logAuthenticateEvent(r *http.Request, profile *identity.Profile) {
if a.cfg.authEventFn == nil {
return
}
state := a.state.Load() state := a.state.Load()
ctx := r.Context() ctx := r.Context()
pub, params, err := hpke.DecryptURLValues(state.hpkePrivateKey, r.Form) pub, params, err := hpke.DecryptURLValues(state.hpkePrivateKey, r.Form)
@ -58,9 +64,14 @@ func (a *Authenticate) logAuthenticateEvent(r *http.Request, profile *identity.P
PubKey: pub.String(), PubKey: pub.String(),
} }
if uid := getUserID(profile); uid != "" { if uid := getUserClaim(profile, "sub"); uid != nil {
uid := getUserID(profile) evt.UID = uid
evt.UID = &uid }
if email := getUserClaim(profile, "email"); email != nil {
evt.Email = email
}
if evt.UID != nil {
evt.Event = AuthEventSignInComplete evt.Event = AuthEventSignInComplete
} else { } else {
evt.Event = AuthEventSignInRequest evt.Event = AuthEventSignInRequest
@ -71,17 +82,20 @@ func (a *Authenticate) logAuthenticateEvent(r *http.Request, profile *identity.P
evt.Domain = &domain evt.Domain = &domain
} }
if a.cfg.authEventFn != nil {
a.cfg.authEventFn(ctx, evt) a.cfg.authEventFn(ctx, evt)
}
} }
func getUserID(profile *identity.Profile) string { func getUserClaim(profile *identity.Profile, field string) *string {
if profile == nil { if profile == nil {
return "" return nil
} }
if profile.Claims == nil { if profile.Claims == nil {
return "" return nil
} }
return profile.Claims.Fields["sub"].GetStringValue() val, ok := profile.Claims.Fields[field]
if !ok || val == nil {
return nil
}
txt := val.GetStringValue()
return &txt
} }