Skip to content

Commit

Permalink
refactor: sort amr claims as most-recent first (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
hf committed Oct 18, 2022
1 parent d061f9f commit 06ac877
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion models/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"database/sql"
"sort"
"time"

"github.com/gobuffalo/pop/v5"
Expand Down Expand Up @@ -37,6 +38,22 @@ type AMREntry struct {
Timestamp int64 `json:"timestamp"`
}

type sortAMREntries struct {
Array []AMREntry
}

func (s sortAMREntries) Len() int {
return len(s.Array)
}

func (s sortAMREntries) Less(i, j int) bool {
return s.Array[i].Timestamp < s.Array[j].Timestamp
}

func (s sortAMREntries) Swap(i, j int) {
s.Array[j], s.Array[i] = s.Array[i], s.Array[j]
}

type Session struct {
ID uuid.UUID `json:"-" db:"id"`
UserID uuid.UUID `json:"user_id" db:"user_id"`
Expand Down Expand Up @@ -146,7 +163,19 @@ func (s *Session) CalculateAALAndAMR() (aal string, amr []AMREntry) {
aal = AAL2.String()
}
amr = append(amr, AMREntry{Method: claim.AuthenticationMethod, Timestamp: claim.UpdatedAt.Unix()})

}

// makes sure that the AMR claims are always ordered most-recent first

// sort in ascending order
sort.Sort(sortAMREntries{
Array: amr,
})

// now reverse for descending order
_ = sort.Reverse(sortAMREntries{
Array: amr,
})

return aal, amr
}

0 comments on commit 06ac877

Please sign in to comment.