diff options
author | Frédéric Guillot <fred@miniflux.net> | 2017-12-16 18:07:53 -0800 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2017-12-16 18:07:53 -0800 |
commit | 00257988ef1707a028cd7dd0b1f8f68e6e8fac53 (patch) | |
tree | f54ccd93abb5054336baea1f0a4a99f8700fc81a /model | |
parent | 58acd1d5e3a997d27f60549b51bdf41df900c6c6 (diff) |
Session management refactoring
Diffstat (limited to 'model')
-rw-r--r-- | model/session.go | 56 | ||||
-rw-r--r-- | model/token.go | 17 |
2 files changed, 56 insertions, 17 deletions
diff --git a/model/session.go b/model/session.go new file mode 100644 index 0000000..2a7430d --- /dev/null +++ b/model/session.go @@ -0,0 +1,56 @@ +// Copyright 2017 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package model + +import ( + "database/sql/driver" + "encoding/json" + "errors" + "fmt" +) + +// SessionData represents the data attached to the session. +type SessionData struct { + CSRF string `json:"csrf"` + OAuth2State string `json:"oauth2_state"` + FlashMessage string `json:"flash_message"` + FlashErrorMessage string `json:"flash_error_message"` +} + +func (s SessionData) String() string { + return fmt.Sprintf(`CSRF="%s", "OAuth2State="%s", FlashMessage="%s", "FlashErrorMessage="%s"`, + s.CSRF, s.OAuth2State, s.FlashMessage, s.FlashErrorMessage) +} + +// Value converts the session data to JSON. +func (s SessionData) Value() (driver.Value, error) { + j, err := json.Marshal(s) + return j, err +} + +// Scan converts raw JSON data. +func (s *SessionData) Scan(src interface{}) error { + source, ok := src.([]byte) + if !ok { + return errors.New("session: unable to assert type of src") + } + + err := json.Unmarshal(source, s) + if err != nil { + return fmt.Errorf("session: %v", err) + } + + return err +} + +// Session represents a session in the system. +type Session struct { + ID string + Data *SessionData +} + +func (s *Session) String() string { + return fmt.Sprintf(`ID="%s", Data="%v"`, s.ID, s.Data) +} diff --git a/model/token.go b/model/token.go deleted file mode 100644 index 3c5c323..0000000 --- a/model/token.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017 Frédéric Guillot. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. - -package model - -import "fmt" - -// Token represents a CSRF token in the system. -type Token struct { - ID string - Value string -} - -func (t Token) String() string { - return fmt.Sprintf(`ID="%s"`, t.ID) -} |