feat: authsession service
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
package testkit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"galaxy/authsession/internal/domain/challenge"
|
||||
"galaxy/authsession/internal/domain/common"
|
||||
"galaxy/authsession/internal/domain/devicesession"
|
||||
"galaxy/authsession/internal/domain/gatewayprojection"
|
||||
)
|
||||
|
||||
func cloneChallenge(record challenge.Challenge) (challenge.Challenge, error) {
|
||||
cloned := record
|
||||
cloned.CodeHash = bytes.Clone(record.CodeHash)
|
||||
cloned.Abuse = cloneAbuseMetadata(record.Abuse)
|
||||
|
||||
if record.Confirmation != nil {
|
||||
confirmation, err := cloneChallengeConfirmation(*record.Confirmation)
|
||||
if err != nil {
|
||||
return challenge.Challenge{}, err
|
||||
}
|
||||
cloned.Confirmation = &confirmation
|
||||
}
|
||||
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
func cloneChallengeConfirmation(value challenge.Confirmation) (challenge.Confirmation, error) {
|
||||
cloned := value
|
||||
|
||||
if value.ClientPublicKey.IsZero() {
|
||||
cloned.ClientPublicKey = common.ClientPublicKey{}
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
key, err := common.NewClientPublicKey(value.ClientPublicKey.PublicKey())
|
||||
if err != nil {
|
||||
return challenge.Confirmation{}, fmt.Errorf("clone challenge confirmation client public key: %w", err)
|
||||
}
|
||||
cloned.ClientPublicKey = key
|
||||
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
func cloneAbuseMetadata(value challenge.AbuseMetadata) challenge.AbuseMetadata {
|
||||
cloned := value
|
||||
if value.LastAttemptAt != nil {
|
||||
lastAttemptAt := *value.LastAttemptAt
|
||||
cloned.LastAttemptAt = &lastAttemptAt
|
||||
}
|
||||
|
||||
return cloned
|
||||
}
|
||||
|
||||
func cloneSession(record devicesession.Session) (devicesession.Session, error) {
|
||||
cloned := record
|
||||
|
||||
if !record.ClientPublicKey.IsZero() {
|
||||
key, err := common.NewClientPublicKey(record.ClientPublicKey.PublicKey())
|
||||
if err != nil {
|
||||
return devicesession.Session{}, fmt.Errorf("clone session client public key: %w", err)
|
||||
}
|
||||
cloned.ClientPublicKey = key
|
||||
}
|
||||
if record.Revocation != nil {
|
||||
revocation := *record.Revocation
|
||||
cloned.Revocation = &revocation
|
||||
}
|
||||
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
func cloneSessions(records []devicesession.Session) ([]devicesession.Session, error) {
|
||||
cloned := make([]devicesession.Session, 0, len(records))
|
||||
for _, record := range records {
|
||||
session, err := cloneSession(record)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cloned = append(cloned, session)
|
||||
}
|
||||
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
func cloneProjectionSnapshot(snapshot gatewayprojection.Snapshot) gatewayprojection.Snapshot {
|
||||
cloned := snapshot
|
||||
if snapshot.RevokedAt != nil {
|
||||
revokedAt := *snapshot.RevokedAt
|
||||
cloned.RevokedAt = &revokedAt
|
||||
}
|
||||
|
||||
return cloned
|
||||
}
|
||||
|
||||
func sortSessionsNewestFirst(records []devicesession.Session) {
|
||||
slices.SortFunc(records, func(left devicesession.Session, right devicesession.Session) int {
|
||||
switch {
|
||||
case left.CreatedAt.Equal(right.CreatedAt):
|
||||
return compareStrings(left.ID.String(), right.ID.String())
|
||||
case left.CreatedAt.After(right.CreatedAt):
|
||||
return -1
|
||||
default:
|
||||
return 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func compareStrings(left string, right string) int {
|
||||
switch {
|
||||
case left < right:
|
||||
return -1
|
||||
case left > right:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func cloneTimePointer(value *time.Time) *time.Time {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
cloned := *value
|
||||
return &cloned
|
||||
}
|
||||
Reference in New Issue
Block a user