Move token generator to backend

This commit is contained in:
nxshock 2019-07-22 19:46:11 +05:00
parent b02c6f99ac
commit 149628e07f
6 changed files with 28 additions and 21 deletions

View File

@ -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}

View File

@ -3,4 +3,5 @@ package memory
const (
groupIDSize = 16
eventIDSize = 16
defaultTokenSize = 16
)

View File

@ -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)
}

View File

@ -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,

View File

@ -2,7 +2,6 @@ package internal
const (
Version = "r0.5.0"
DefaultTokenSize = 16
)
// https://matrix.org/docs/spec/client_server/latest#authentication-types

View File

@ -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 "