mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-23 19:02:19 +00:00
Move token generator to backend
This commit is contained in:
parent
b02c6f99ac
commit
149628e07f
@ -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}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package memory
|
||||
|
||||
const (
|
||||
groupIDSize = 16
|
||||
eventIDSize = 16
|
||||
groupIDSize = 16
|
||||
eventIDSize = 16
|
||||
defaultTokenSize = 16
|
||||
)
|
||||
|
17
internal/backends/memory/tokens.go
Normal file
17
internal/backends/memory/tokens.go
Normal 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)
|
||||
}
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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 "
|
||||
|
Loading…
Reference in New Issue
Block a user