Skip to content

Commit

Permalink
Change userID to be int64 across all platform
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimschmidt557 committed Dec 12, 2023
1 parent 2be2f75 commit 4e4afaa
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions data/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// It contains the unique client id and an interval diff, stating added and removed intervals as strings.
// It is (and should) only be used for JSON parsing.
type JSONRequest struct {
UserID int `json:"userID"`
UserID int64 `json:"userID"`
Added []JSONInterval `json:"added"`
Removed []JSONInterval `json:"removed"`
}
Expand All @@ -35,7 +35,7 @@ type JSONRequest struct {
// It contains the id of the user who is syncing
// and its interval diff.
type SyncRequest struct {
UserID int
UserID int64
Added []Interval
Removed []Interval
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var keyDirectoryPath string
var dbPath string
var noAuth bool
var sourcePath string
var userID int
var userID int64

func main() {
startCmd := flag.NewFlagSet("start", flag.ExitOnError)
Expand All @@ -54,7 +54,7 @@ func main() {
addUserCmd.StringVar(&keyDirectoryPath, "keys-location", "authorized_keys", "Path to the users' public keys")

addKeyCmd.StringVar(&sourcePath, "path", "", "Supply the path to a PEM RSA key")
addKeyCmd.IntVar(&userID, "id", -1, "Supply user id")
addKeyCmd.Int64Var(&userID, "id", -1, "Supply user id")
addKeyCmd.StringVar(&keyDirectoryPath, "keys-location", "authorized_keys", "Path to the users' public keys")

flag.BoolVar(&versionFlag, "version", false, "Print version information")
Expand Down
2 changes: 1 addition & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// A UserId represents a unique ID assigned to each user of the
// timewarrior sync server
type UserId int
type UserId int64

type IntervalKey struct {
Start time.Time
Expand Down
6 changes: 3 additions & 3 deletions sync/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Authenticate(r *http.Request, body data.SyncRequest) bool {

// AuthenticateWithKeySet returns true iff the JWT in the Bearer token can be validated in verified with a key in the
// given key set
func AuthenticateWithKeySet(r *http.Request, userID int, keySet jwk.Set) bool {
func AuthenticateWithKeySet(r *http.Request, userID int64, keySet jwk.Set) bool {
for i := 0; i < keySet.Len(); i++ {
key, ok := keySet.Get(i)
if !ok {
Expand All @@ -63,7 +63,7 @@ func AuthenticateWithKeySet(r *http.Request, userID int, keySet jwk.Set) bool {
}

presumedUserID, ok := id.(float64)
if !ok || int(presumedUserID) != userID {
if !ok || int64(presumedUserID) != userID {
continue
}
return true
Expand All @@ -73,7 +73,7 @@ func AuthenticateWithKeySet(r *http.Request, userID int, keySet jwk.Set) bool {

// GetKeySet returns the key set of user with a given userId. Returns an error if the keys file of that user was not
// found or could not be parsed.
func GetKeySet(userId int) (jwk.Set, error) {
func GetKeySet(userId int64) (jwk.Set, error) {
filename := fmt.Sprintf("%d_keys", userId)
path := filepath.Join(PublicKeyLocation, filename)

Expand Down
2 changes: 1 addition & 1 deletion sync/conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// SolveConflict merges overlapping intervals of given user.
// It then updates userId's state in store accordingly
// SolveConflict returns true iff a conflict was detected
func SolveConflict(userId int, store storage.Storage) (bool, error) {
func SolveConflict(userId int64, store storage.Storage) (bool, error) {
conflictDetected := false
intervals, err := store.GetIntervals(storage.UserId(userId))

Expand Down
12 changes: 6 additions & 6 deletions sync/user_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ import (

// GetUsedUserIDs returns a map containing every user id with an existing file [user id]_keys
// in PublicKeyLocation directory
func GetUsedUserIDs() map[int]bool {
func GetUsedUserIDs() map[int64]bool {
files, err := ioutil.ReadDir(PublicKeyLocation)
if err != nil {
log.Fatal("Error accessing keys-location directory")
}
used := make(map[int]bool)
used := make(map[int64]bool)

for _, f := range files {
s := strings.Split(f.Name(), "_")
if len(s) != 2 {
continue
}
i, err := strconv.Atoi(s[0])
i, err := strconv.ParseInt(s[0], 10, 64)
if err != nil {
continue
}
Expand All @@ -54,9 +54,9 @@ func GetUsedUserIDs() map[int]bool {
}

// GetFreeUserID returns the smallest valid unused user id
func GetFreeUserID() int {
func GetFreeUserID() int64 {
used := GetUsedUserIDs()
for i := 0; i <= math.MaxInt64; i++ {
for i := int64(0); i <= math.MaxInt64; i++ {
if !used[i] {
return i
}
Expand All @@ -75,7 +75,7 @@ func ReadKey(path string) string {
}

// AddKey adds the given key to the key file of the given user
func AddKey(userID int, key string) {
func AddKey(userID int64, key string) {
if userID < 0 {
log.Fatal("Error adding key. Negative user id not allowed")
}
Expand Down

0 comments on commit 4e4afaa

Please sign in to comment.