forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
62 lines (51 loc) · 1.62 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package service
import (
"errors"
"golang.org/x/net/context"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
rpc "github.com/keybase/go-framed-msgpack-rpc"
)
var ErrNoSession = errors.New("no current session")
// SessionHandler is the RPC handler for the session interface.
type SessionHandler struct {
libkb.Contextified
*BaseHandler
}
// NewSessionHandler creates a SessionHandler for the xp transport.
func NewSessionHandler(xp rpc.Transporter, g *libkb.GlobalContext) *SessionHandler {
return &SessionHandler{
BaseHandler: NewBaseHandler(xp),
Contextified: libkb.NewContextified(g),
}
}
// CurrentSession uses the global session to find the session. If
// the user isn't logged in, it returns ErrNoSession.
func (h *SessionHandler) CurrentSession(_ context.Context, sessionID int) (keybase1.Session, error) {
var s keybase1.Session
var token string
var username libkb.NormalizedUsername
var uid keybase1.UID
var deviceSubkey, deviceSibkey libkb.GenericKey
var err error
aerr := h.G().LoginState().Account(func(a *libkb.Account) {
uid, username, token, deviceSubkey, deviceSibkey, err = a.UserInfo()
}, "Service - SessionHandler - UserInfo")
if aerr != nil {
return s, aerr
}
if err != nil {
if _, ok := err.(libkb.LoginRequiredError); ok {
return s, ErrNoSession
}
return s, err
}
s.Uid = uid
s.Username = username.String()
s.Token = token
s.DeviceSubkeyKid = deviceSubkey.GetKID()
s.DeviceSibkeyKid = deviceSibkey.GetKID()
return s, nil
}