mirror of
https://github.com/diamondburned/cchat-mock.git
synced 2025-07-12 22:35:52 +00:00
Implemented SessionSaver and SessionRestorer
This commit is contained in:
parent
807afeffb7
commit
bc9f49c387
2
go.mod
2
go.mod
|
@ -4,5 +4,5 @@ go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Pallinder/go-randomdata v1.2.0
|
github.com/Pallinder/go-randomdata v1.2.0
|
||||||
github.com/diamondburned/cchat v0.0.9
|
github.com/diamondburned/cchat v0.0.10
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -18,4 +18,6 @@ github.com/diamondburned/cchat v0.0.8 h1:/PmI23SFHJcjYBWNBwQbp36n7fDvDu+NMnQuhM5
|
||||||
github.com/diamondburned/cchat v0.0.8/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
github.com/diamondburned/cchat v0.0.8/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||||
github.com/diamondburned/cchat v0.0.9 h1:+F96eDDuaOg4v4dz3GBDWbEW4dZ/k5uGrDp33/yeXR8=
|
github.com/diamondburned/cchat v0.0.9 h1:+F96eDDuaOg4v4dz3GBDWbEW4dZ/k5uGrDp33/yeXR8=
|
||||||
github.com/diamondburned/cchat v0.0.9/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
github.com/diamondburned/cchat v0.0.9/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||||
|
github.com/diamondburned/cchat v0.0.10 h1:aiUVgGre5E/HV+Iw6tmBVbuGctQI+JndV9nIDoYuRPY=
|
||||||
|
github.com/diamondburned/cchat v0.0.10/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
|
41
service.go
41
service.go
|
@ -3,6 +3,7 @@ package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/diamondburned/cchat"
|
"github.com/diamondburned/cchat"
|
||||||
|
@ -13,17 +14,30 @@ func init() {
|
||||||
services.RegisterService(&Service{})
|
services.RegisterService(&Service{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrInvalidSession is returned if SessionRestore is given a bad session.
|
||||||
|
var ErrInvalidSession = errors.New("invalid session")
|
||||||
|
|
||||||
type Service struct{}
|
type Service struct{}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ cchat.Service = (*Service)(nil)
|
_ cchat.Service = (*Service)(nil)
|
||||||
_ cchat.Configurator = (*Service)(nil)
|
_ cchat.Configurator = (*Service)(nil)
|
||||||
|
_ cchat.SessionRestorer = (*Service)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s Service) Name() string {
|
func (s Service) Name() string {
|
||||||
return "Mock"
|
return "Mock"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Service) RestoreSession(storage map[string]string) (cchat.Session, error) {
|
||||||
|
username, ok := storage["username"]
|
||||||
|
if !ok {
|
||||||
|
return nil, ErrInvalidSession
|
||||||
|
}
|
||||||
|
|
||||||
|
return newSession(username), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s Service) Authenticate() cchat.Authenticator {
|
func (s Service) Authenticate() cchat.Authenticator {
|
||||||
return Authenticator{}
|
return Authenticator{}
|
||||||
}
|
}
|
||||||
|
@ -39,9 +53,7 @@ func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
||||||
ses := &Session{username: form[0]}
|
return newSession(form[0]), nil
|
||||||
ses.servers = GenerateServers(ses)
|
|
||||||
return ses, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -87,10 +99,19 @@ func unmarshalConfig(config map[string]string, key string, value interface{}) er
|
||||||
type Session struct {
|
type Session struct {
|
||||||
username string
|
username string
|
||||||
servers []cchat.Server
|
servers []cchat.Server
|
||||||
lastid uint32
|
lastid uint32 // used for generation
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ cchat.Session = (*Session)(nil)
|
var (
|
||||||
|
_ cchat.Session = (*Session)(nil)
|
||||||
|
_ cchat.SessionSaver = (*Session)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
func newSession(username string) *Session {
|
||||||
|
ses := &Session{username: username}
|
||||||
|
ses.servers = GenerateServers(ses)
|
||||||
|
return ses
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Session) Name() (string, error) {
|
func (s *Session) Name() (string, error) {
|
||||||
return s.username, nil
|
return s.username, nil
|
||||||
|
@ -100,3 +121,9 @@ func (s *Session) Servers(container cchat.ServersContainer) error {
|
||||||
container.SetServers(s.servers)
|
container.SetServers(s.servers)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) Save() (map[string]string, error) {
|
||||||
|
return map[string]string{
|
||||||
|
"username": s.username,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue