mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-24 03:12:20 +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")
|
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}
|
||||||
|
|
||||||
|
@ -3,4 +3,5 @@ package memory
|
|||||||
const (
|
const (
|
||||||
groupIDSize = 16
|
groupIDSize = 16
|
||||||
eventIDSize = 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{
|
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,
|
||||||
|
@ -2,7 +2,6 @@ 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
|
||||||
|
@ -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 "
|
||||||
|
Loading…
Reference in New Issue
Block a user