Skip to content

Commit

Permalink
httpaccess auth device api.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruizeng committed Nov 19, 2015
1 parent d269d85 commit 447cb1d
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 8 deletions.
9 changes: 9 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ func RPCCallByName(serverName string, serverMethod string, args interface{}, rep
return serverInstance.rpccli.Call(serverName, serverMethod, args, reply)
}

// get server's hosts by server name and service type
func GetServerHosts(serverName string, hostType string) ([]string, error) {
if serverInstance == nil {
return nil, errorf(errServerNotInit)
}

return serverInstance.svrmgr.GetServerHosts(serverName, hostType)
}

// start service
func Run() error {
if serverInstance == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (t *testTimer) DoTask() {
}

func validateGetServerHosts(t *testing.T, flag string, want string) {
hosts, err := serverInstance.svrmgr.GetServerHosts("test", flag)
hosts, err := GetServerHosts("test", flag)
if err != nil {
t.Error(err)
}
Expand Down
1 change: 1 addition & 0 deletions services/apiprovider/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions services/apiprovider/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions services/apiprovider/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
62 changes: 60 additions & 2 deletions services/httpaccess/actions.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package main

import (
"encoding/hex"
"errors"
"github.com/PandoCloud/pando-cloud/pkg/models"
"github.com/PandoCloud/pando-cloud/pkg/rpcs"
"github.com/PandoCloud/pando-cloud/pkg/server"
"github.com/PandoCloud/pando-cloud/pkg/token"
"github.com/martini-contrib/render"
"math/rand"
"net/http"
)

const (
ErrOK = 0
ErrSystemFault = 10001
ErrOK = 0
ErrSystemFault = 10001
ErrDeviceNotFound = 10002
ErrWrongSecret = 10003
ErrProtocolNotSuported = 10004
)

func renderError(code int, err error) Common {
Expand Down Expand Up @@ -60,3 +67,54 @@ func RegisterDevice(args DeviceRegisterArgs, r render.Render) {
r.JSON(http.StatusOK, result)
return
}

func AuthDevice(args DeviceAuthArgs, r render.Render) {
server.Log.Printf("ACTION AuthDevice, args:: %v", args)
device := &models.Device{}
err := server.RPCCallByName("registry", "Registry.FindDeviceById", int64(args.DeviceId), device)
if err != nil {
r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
return
}

if device.DeviceSecret != args.DeviceSecret {
// device secret is wrong.
r.JSON(http.StatusOK, renderError(ErrWrongSecret, errors.New("wrong device secret.")))
return
}

hepler := token.NewHelper(*confRedisHost)
token, err := hepler.GenerateToken(uint64(device.ID))
if err != nil {
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
return
}

var hosts []string
switch args.Protocol {
case "http":
hosts, err = server.GetServerHosts(args.Protocol+"access", "httphost")
case "mqtt":
hosts, err = server.GetServerHosts(args.Protocol+"access", "tcphost")
default:
err = errors.New("unsuported protocol: " + args.Protocol)
}
if err != nil {
r.JSON(http.StatusOK, renderError(ErrProtocolNotSuported, err))
return
}

// just get a random host
host := hosts[rand.Intn(len(hosts))]

result := DeviceAuthResponse{}
result.Data = DeviceAuthData{
AccessToken: hex.EncodeToString(token),
AccessAddr: host,
}

server.Log.Infof("auth device success: %v, token: %x, access: %v", device, token, host)

r.JSON(http.StatusOK, result)
return
}
15 changes: 15 additions & 0 deletions services/httpaccess/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"flag"
)

const (
flagRedisHost = "redishost"

defaultRedisHost = "localhost:6379"
)

var (
confRedisHost = flag.String(flagRedisHost, defaultRedisHost, "redis host address, ip:port")
)
6 changes: 2 additions & 4 deletions services/httpaccess/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ type DeviceRegisterResponse struct {

// device auth response data field
type DeviceAuthData struct {
AccessToken string `json:"access_token"`
AccessAddr string `json:"access_addr"`
EventSequence uint64 `json:"event_sequence"`
DataSequence uint64 `json:"data_sequence"`
AccessToken string `json:"access_token"`
AccessAddr string `json:"access_addr"`
}

// device auth response
Expand Down
2 changes: 1 addition & 1 deletion services/httpaccess/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ func route(m *martini.ClassicMartini) {
m.Post("/v1/devices/registration", binding.Json(DeviceRegisterArgs{}), RegisterDevice)

// auth device
// m.Post("v1/devices/authentication", binding.Json(DeviceAuthArgs{}), actions.AuthDevice)
m.Post("/v1/devices/authentication", binding.Json(DeviceAuthArgs{}), AuthDevice)

}
17 changes: 17 additions & 0 deletions services/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ func (r *Registry) FindDeviceByIdentifier(identifier string, reply *models.Devic
return nil
}

// FindDeviceById will find the device with given id
func (r *Registry) FindDeviceById(id int64, reply *models.Device) error {
db, err := getDB()
if err != nil {
return err
}

err = db.Where(&models.Device{
ID: id,
}).First(reply).Error

if err != nil {
return err
}
return nil
}

// UpdateDevice will update a device info by identifier
func (r *Registry) UpdateDeviceInfo(args *rpcs.ArgsDeviceUpdate, reply *models.Device) error {
db, err := getDB()
Expand Down
17 changes: 17 additions & 0 deletions services/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ func testDevice(t *testing.T, r *Registry) {
}
t.Log(device)

founddev := &models.Device{}
err = r.FindDeviceById(device.ID, founddev)
if err != nil {
t.Error(err)
}
if device.DeviceIdentifier != founddev.DeviceIdentifier {
t.Errorf("FindDeviceById not match, want %v, got %v", device, founddev)
}

err = r.FindDeviceByIdentifier(device.DeviceIdentifier, founddev)
if err != nil {
t.Error(err)
}
if device.ID != founddev.ID {
t.Errorf("FindDeviceByIdentifier not match, want %v, got %v", device, founddev)
}

device.DeviceDescription = "test change device info."

args2 := &rpcs.ArgsDeviceUpdate{
Expand Down

0 comments on commit 447cb1d

Please sign in to comment.