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") 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{ user = &User{
name: username, name: username,
@ -62,7 +62,7 @@ func (backend *Backend) Login(username, password, device string) (token string,
return "", internal.NewError(models.M_FORBIDDEN, "wrong password") 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} backend.data[username].(*User).Tokens[token] = Token{Device: device}

View File

@ -1,6 +1,7 @@
package memory package memory
const ( const (
groupIDSize = 16 groupIDSize = 16
eventIDSize = 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{ events = append(events, RoomEvent{
Content: nil, Content: nil,
Type: rooms.Create, Type: rooms.Create,
EventID: internal.NewToken(eventIDSize), EventID: newToken(eventIDSize),
Sender: user, Sender: user,
OriginServerTS: t}) OriginServerTS: t})
@ -54,7 +54,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models
events = append(events, RoomEvent{ events = append(events, RoomEvent{
Content: []byte(request.Visibility), // TODO: check visibility vs join rules Content: []byte(request.Visibility), // TODO: check visibility vs join rules
Type: rooms.JoinRules, Type: rooms.JoinRules,
EventID: internal.NewToken(eventIDSize), EventID: newToken(eventIDSize),
Sender: user, Sender: user,
OriginServerTS: t}) OriginServerTS: t})
@ -63,7 +63,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models
events = append(events, RoomEvent{ events = append(events, RoomEvent{
Content: nil, // TODO: add Content: nil, // TODO: add
Type: rooms.Name, Type: rooms.Name,
EventID: internal.NewToken(eventIDSize), EventID: newToken(eventIDSize),
Sender: user, Sender: user,
OriginServerTS: t}) OriginServerTS: t})
} }
@ -73,13 +73,13 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models
events = append(events, RoomEvent{ events = append(events, RoomEvent{
Content: nil, // TODO: add Content: nil, // TODO: add
Type: rooms.CanonicalAlias, Type: rooms.CanonicalAlias,
EventID: internal.NewToken(eventIDSize), EventID: newToken(eventIDSize),
Sender: user, Sender: user,
OriginServerTS: t}) OriginServerTS: t})
} }
room := &Room{ room := &Room{
id: internal.NewToken(groupIDSize), id: newToken(groupIDSize),
aliasName: request.RoomAliasName, aliasName: request.RoomAliasName,
name: request.Name, name: request.Name,
topic: request.Topic, topic: request.Topic,

View File

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

View File

@ -1,20 +1,10 @@
package internal package internal
import ( import (
"crypto/rand"
"fmt"
"net/http" "net/http"
"strings" "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. // getTokenFromResponse returns token from request.
func getTokenFromResponse(r *http.Request) string { func getTokenFromResponse(r *http.Request) string {
const prefix = "Bearer " const prefix = "Bearer "