Skip to content

Commit

Permalink
inital implementation of manager login/logout
Browse files Browse the repository at this point in the history
  • Loading branch information
林志宇 committed Mar 22, 2019
1 parent 3fb368e commit 2963076
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 255 deletions.
5 changes: 4 additions & 1 deletion cmd/manager-node/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ var rootCmd = &cobra.Command{
log.Println("SK:", sk)
},
Run: func(_ *cobra.Command, _ []string) {
m := manager.NewNode(pk, sk)
m, err := manager.NewNode(manager.Config{}) // TODO: complete
if err != nil {
log.Fatalln("Failed to start manager:", err)
}
go func() {
l, err := net.Listen("tcp", rpcAddr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6
github.com/go-chi/chi v4.0.2+incompatible
github.com/google/uuid v1.1.1
github.com/gorilla/sessions v1.1.3 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAU
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.1.3 h1:uXoZdcdA5XdXF3QzuSlheVRUvjl+1rKY7zBXL68L9RU=
github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down
50 changes: 0 additions & 50 deletions pkg/manager/auth.go

This file was deleted.

196 changes: 0 additions & 196 deletions pkg/manager/auth_store.go

This file was deleted.

37 changes: 29 additions & 8 deletions pkg/manager/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,41 @@ import (
"github.com/skycoin/skywire/pkg/routing"
)

type Config struct {
PK cipher.PubKey
SK cipher.SecKey
Users UsersConfig
Sessions SessionsConfig
}

// Node manages AppNodes.
type Node struct {
pk cipher.PubKey
sk cipher.SecKey
c Config
nodes map[cipher.PubKey]node.RPCClient // connected remote nodes.
users UserStorer
sessions *SessionsManager
mu *sync.RWMutex
}

// NewNode creates a new Node.
func NewNode(pk cipher.PubKey, sk cipher.SecKey) *Node {
func NewNode(config Config) (*Node, error) {
users, err := NewBoltUserStore(config.Users)
if err != nil {
return nil, err
}
return &Node{
pk: pk,
sk: sk,
c: config,
nodes: make(map[cipher.PubKey]node.RPCClient),
users: users,
sessions: NewSessionsManager(users, config.Sessions),
mu: new(sync.RWMutex),
}
}, nil
}

// ServeRPC serves RPC of a Node.
func (m *Node) ServeRPC(lis net.Listener) error {
for {
conn, err := noise.WrapListener(lis, m.pk, m.sk, false, noise.HandshakeXK).Accept()
conn, err := noise.WrapListener(lis, m.c.PK, m.c.SK, false, noise.HandshakeXK).Accept()
if err != nil {
return err
}
Expand Down Expand Up @@ -92,12 +105,14 @@ func (m *Node) ServeHTTP(w http.ResponseWriter, r *http.Request) {

func (m *Node) authHandler() http.Handler {
r := chi.NewRouter()
r.Post("/login", m.sessions.Login())
r.Post("/logout", m.sessions.Logout())
return r
}

func (m *Node) apiHandler() http.Handler {
r := chi.NewRouter()
r.Use() // TODO: Complete!
r.Use(m.sessions.Authorize)

r.Get("/nodes", m.getNodes())
r.Get("/nodes/{pk}", m.getNode())
Expand Down Expand Up @@ -533,3 +548,9 @@ func (m *Node) ctxRoute(next routeHandlerFunc) http.HandlerFunc {
next(w, r, routeCtx{nodeCtx: ctx, RtKey: rid})
})
}

func catch(err error) {
if err != nil {
panic(err)
}
}
Loading

0 comments on commit 2963076

Please sign in to comment.