🎨 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.
type Profile struct {
Photo []*URL
URL []*URL
Email []*Email
Name []string
Photo []*URL `json:"photo"`
URL []*URL `json:"url"`
Email []*Email `json:"email"`
Name []string `json:"name"`
}
func NewProfile() *Profile {
@ -67,4 +67,4 @@ func (p Profile) GetEmail() *Email {
}
return p.Email[0]
}
}

View File

@ -6,15 +6,16 @@ import (
"source.toby3d.me/website/indieauth/internal/random"
)
//nolint: tagliatelle
type Session struct {
ClientID *ClientID
RedirectURI *URL
Me *Me
Profile *Profile
Scope Scopes
CodeChallengeMethod CodeChallengeMethod
CodeChallenge string
Code string
ClientID *ClientID `json:"client_id"`
RedirectURI *URL `json:"redirect_uri"`
Me *Me `json:"me"`
Profile *Profile `json:"profile,omitempty"`
Scope Scopes `json:"scope"`
CodeChallengeMethod CodeChallengeMethod `json:"code_challenge_method,omitempty"`
CodeChallenge string `json:"code_challenge,omitempty"`
Code string `json:"-"`
}
// 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
}

View File

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