1package signin
2
3import (
4 "errors"
5 "fmt"
6 "net/http"
7
8 "tug/internal/service/session"
9 transporthttp "tug/internal/transport/http"
10 "tug/internal/transport/oidcrp"
11 "tug/internal/transport/web"
12)
13
14func (l login) openAccount(w http.ResponseWriter, r *http.Request, userID, via, client string, epoch int64, browser string) error {
15 ctx := r.Context()
16 if secret, ok := l.rp.SessionSecret(r); ok {
17 old, err := l.s.Resolve(ctx, secret, l.host)
18 if err == nil {
19 err = l.s.Revoke(ctx, old)
20 }
21 if err != nil && !errors.Is(err, session.ErrNoSession) {
22 return fmt.Errorf("%w: replace the account session: %w", web.ErrLoginBusy, err)
23 }
24 }
25 c := transporthttp.ClientOf(ctx)
26 secret, p, err := l.s.Open(ctx, session.OpenInput{UserID: userID, Host: l.host, Via: via, ClientID: client, Epoch: &epoch, Browser: browser, Address: c.Addr, UserAgent: c.UserAgent})
27 if errors.Is(err, session.ErrNoSession) {
28 return fmt.Errorf("%w: open the account session: %w", web.ErrLoginStale, err)
29 }
30 if err != nil {
31 return fmt.Errorf("%w: open the account session: %w", web.ErrLoginBusy, err)
32 }
33 l.rp.SetSession(w, secret, oidcrp.Session{UserID: p.UserID, Expires: p.ExpiresAt})
34 return nil
35}