Beispiel #1
0
func NewDeviceManager(redishost string) *DeviceManager {
	mgr := online.NewManager(redishost)

	helper := token.NewHelper(redishost)

	return &DeviceManager{
		onlineManager: mgr,
		tokenHelper:   helper,
	}
}
Beispiel #2
0
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
}