🎨 Format session logic code

This commit is contained in:
Maxim Lebedev 2022-02-18 00:13:45 +05:00
parent 6867e41fdf
commit 1480f58cac
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
4 changed files with 21 additions and 16 deletions

View File

@ -6,10 +6,10 @@ import (
// Profile describes the data about the user. // Profile describes the data about the user.
type Profile struct { type Profile struct {
Photo []*URL Photo []*URL `json:"photo"`
URL []*URL URL []*URL `json:"url"`
Email []*Email Email []*Email `json:"email"`
Name []string Name []string `json:"name"`
} }
func NewProfile() *Profile { func NewProfile() *Profile {
@ -67,4 +67,4 @@ func (p Profile) GetEmail() *Email {
} }
return p.Email[0] return p.Email[0]
} }

View File

@ -6,15 +6,16 @@ import (
"source.toby3d.me/website/indieauth/internal/random" "source.toby3d.me/website/indieauth/internal/random"
) )
//nolint: tagliatelle
type Session struct { type Session struct {
ClientID *ClientID ClientID *ClientID `json:"client_id"`
RedirectURI *URL RedirectURI *URL `json:"redirect_uri"`
Me *Me Me *Me `json:"me"`
Profile *Profile Profile *Profile `json:"profile,omitempty"`
Scope Scopes Scope Scopes `json:"scope"`
CodeChallengeMethod CodeChallengeMethod CodeChallengeMethod CodeChallengeMethod `json:"code_challenge_method,omitempty"`
CodeChallenge string CodeChallenge string `json:"code_challenge,omitempty"`
Code string Code string `json:"-"`
} }
// TestSession returns valid random generated session for tests. // TestSession returns valid random generated session for tests.

View File

@ -86,5 +86,9 @@ func (repo *httpProfileRepository) Get(ctx context.Context, me *domain.Me) (*dom
} }
} }
if result.GetName() == "" && result.GetURL() == nil && result.GetPhoto() == nil && result.GetEmail() == nil {
return nil, profile.ErrNotExist
}
return result, nil return result, nil
} }

View File

@ -124,7 +124,7 @@ func (repo *sqlite3SessionRepository) GC() {}
func NewSession(src *domain.Session) (*Session, error) { func NewSession(src *domain.Session) (*Session, error) {
data, err := json.Marshal(src) data, err := json.Marshal(src)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("cannot encode data to JSON: %w", err)
} }
return &Session{ return &Session{
@ -142,11 +142,11 @@ func (t *Session) Populate(src []byte, dst *domain.Session) error {
n, err := base64.StdEncoding.Decode(tmp, src) n, err := base64.StdEncoding.Decode(tmp, src)
if err != nil { if err != nil {
return err return fmt.Errorf("cannot decode base64 data: %w", err)
} }
if err = json.Unmarshal(tmp[:n], dst); err != nil { if err = json.Unmarshal(tmp[:n], dst); err != nil {
return err return fmt.Errorf("cannot decode JSON data: %w", err)
} }
return nil return nil