From 149628e07f03ebcee06191bde440e24add439742 Mon Sep 17 00:00:00 2001 From: nxshock Date: Mon, 22 Jul 2019 19:46:11 +0500 Subject: [PATCH] Move token generator to backend --- internal/backends/memory/backend.go | 4 ++-- internal/backends/memory/consts.go | 5 +++-- internal/backends/memory/tokens.go | 17 +++++++++++++++++ internal/backends/memory/user.go | 10 +++++----- internal/consts.go | 3 +-- internal/tokenutils.go | 10 ---------- 6 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 internal/backends/memory/tokens.go diff --git a/internal/backends/memory/backend.go b/internal/backends/memory/backend.go index 51b0590..0a160e9 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -34,7 +34,7 @@ func (backend *Backend) Register(username, password, device string) (user intern return nil, "", internal.NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken") } - token = internal.NewToken(internal.DefaultTokenSize) + token = newToken(defaultTokenSize) user = &User{ name: username, @@ -62,7 +62,7 @@ func (backend *Backend) Login(username, password, device string) (token string, return "", internal.NewError(models.M_FORBIDDEN, "wrong password") } - token = internal.NewToken(internal.DefaultTokenSize) + token = newToken(defaultTokenSize) backend.data[username].(*User).Tokens[token] = Token{Device: device} diff --git a/internal/backends/memory/consts.go b/internal/backends/memory/consts.go index 2af747d..082a68d 100644 --- a/internal/backends/memory/consts.go +++ b/internal/backends/memory/consts.go @@ -1,6 +1,7 @@ package memory const ( - groupIDSize = 16 - eventIDSize = 16 + groupIDSize = 16 + eventIDSize = 16 + defaultTokenSize = 16 ) diff --git a/internal/backends/memory/tokens.go b/internal/backends/memory/tokens.go new file mode 100644 index 0000000..0d5b0e3 --- /dev/null +++ b/internal/backends/memory/tokens.go @@ -0,0 +1,17 @@ +package memory + +import ( + "crypto/rand" + "fmt" +) + +// newToken returns new generated token with specified length +func newToken(size int) string { + b := make([]byte, size) + _, err := rand.Read(b) + if err != nil { + panic(err) + } + + return fmt.Sprintf("%x", b) +} diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 37a6761..2c1eb8b 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -44,7 +44,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models events = append(events, RoomEvent{ Content: nil, Type: rooms.Create, - EventID: internal.NewToken(eventIDSize), + EventID: newToken(eventIDSize), Sender: user, OriginServerTS: t}) @@ -54,7 +54,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models events = append(events, RoomEvent{ Content: []byte(request.Visibility), // TODO: check visibility vs join rules Type: rooms.JoinRules, - EventID: internal.NewToken(eventIDSize), + EventID: newToken(eventIDSize), Sender: user, OriginServerTS: t}) @@ -63,7 +63,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models events = append(events, RoomEvent{ Content: nil, // TODO: add Type: rooms.Name, - EventID: internal.NewToken(eventIDSize), + EventID: newToken(eventIDSize), Sender: user, OriginServerTS: t}) } @@ -73,13 +73,13 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models events = append(events, RoomEvent{ Content: nil, // TODO: add Type: rooms.CanonicalAlias, - EventID: internal.NewToken(eventIDSize), + EventID: newToken(eventIDSize), Sender: user, OriginServerTS: t}) } room := &Room{ - id: internal.NewToken(groupIDSize), + id: newToken(groupIDSize), aliasName: request.RoomAliasName, name: request.Name, topic: request.Topic, diff --git a/internal/consts.go b/internal/consts.go index d88649f..4012a9d 100644 --- a/internal/consts.go +++ b/internal/consts.go @@ -1,8 +1,7 @@ package internal const ( - Version = "r0.5.0" - DefaultTokenSize = 16 + Version = "r0.5.0" ) // https://matrix.org/docs/spec/client_server/latest#authentication-types diff --git a/internal/tokenutils.go b/internal/tokenutils.go index bacc599..a437266 100644 --- a/internal/tokenutils.go +++ b/internal/tokenutils.go @@ -1,20 +1,10 @@ package internal import ( - "crypto/rand" - "fmt" "net/http" "strings" ) -// newToken returns new generated token with specified length -func NewToken(size int) string { - b := make([]byte, size) - rand.Read(b) - - return fmt.Sprintf("%x", b) -} - // getTokenFromResponse returns token from request. func getTokenFromResponse(r *http.Request) string { const prefix = "Bearer "