112 lines
3.8 KiB
Go
112 lines
3.8 KiB
Go
// Code generated by zi2go from device_http.zi. DO NOT EDIT.
|
|
package main
|
|
|
|
// #import std_go_types
|
|
// #import device_keys
|
|
type Devices struct {
|
|
Database *Database
|
|
Configuration *Config
|
|
Counters *ServerMetrics
|
|
Verify VerifySignature
|
|
ReplayError Error
|
|
}
|
|
|
|
type DevicesResponse struct {
|
|
Devices []DeviceKey "json:\"devices\""
|
|
}
|
|
|
|
type DeviceRevocationResponse struct {
|
|
Status string "json:\"status\""
|
|
}
|
|
|
|
func DeviceHttp_Route(devices Devices, writer ResponseWriter, request *Request) {
|
|
user := HttpAuth_BearerUser(devices.Database, request, devices.Configuration.TokenSecret)
|
|
if user.Authentication.Error != nil || user.Authentication.Status != int(0) {
|
|
HttpAuth_Respond(writer, devices.Counters, user.Authentication)
|
|
return
|
|
}
|
|
method := StdHttpGo_Method(request)
|
|
if method == "GET" {
|
|
listed := DeviceKeys_List(devices.Database, StdHttpGo_Context(request), user.Value)
|
|
if listed.Error != nil {
|
|
Response_Error(writer, int(500), "device list failed")
|
|
return
|
|
}
|
|
var value DevicesResponse = DevicesResponse{}
|
|
value.Devices = listed.Value
|
|
Response_JSON(writer, int(200), value)
|
|
return
|
|
}
|
|
if method == "DELETE" {
|
|
DeviceHttp_Revoke(devices, writer, request, user.Value)
|
|
return
|
|
}
|
|
body := HttpBody_ReadJSON(writer, request, devices.Configuration.MaxBodyBytes)
|
|
if body.Error != nil {
|
|
Response_Error(writer, int(400), StdErrorsGo_Message(body.Error))
|
|
return
|
|
}
|
|
var registration DeviceRegistrationRequest = DeviceRegistrationRequest{}
|
|
var value_0 Error = StdJsonGo_Unmarshal(body.Value, &(registration))
|
|
if value_0 != nil {
|
|
Response_Error(writer, int(400), "invalid device registration")
|
|
return
|
|
}
|
|
DeviceKeys_NormalizeRegistration(&(registration))
|
|
context := StdHttpGo_Context(request)
|
|
verified := DeviceKeys_VerifyRegistration(devices.Database, context, user.Value, registration, devices.Verify)
|
|
if verified.Error != nil || verified.Status != int(0) {
|
|
HttpAuth_Respond(writer, devices.Counters, verified)
|
|
return
|
|
}
|
|
var device DeviceKey = DeviceKey{}
|
|
device.AccountID = user.Value
|
|
device.AppID = registration.AppID
|
|
device.KeyID = registration.KeyID
|
|
device.ClientID = registration.ClientID
|
|
device.PublicKey = registration.PublicKey
|
|
error := DeviceKeys_Register(devices.Database, context, device, registration.Nonce, devices.ReplayError)
|
|
if error != nil {
|
|
if StdErrorsGo_Is(error, devices.ReplayError) {
|
|
Response_Error(writer, int(409), "device registration replay")
|
|
return
|
|
}
|
|
Response_Error(writer, int(500), "device registration failed")
|
|
return
|
|
}
|
|
Response_JSON(writer, int(200), device)
|
|
}
|
|
|
|
func DeviceHttp_Revoke(devices Devices, writer ResponseWriter, request *Request, accountID string) {
|
|
body := HttpBody_ReadJSON(writer, request, devices.Configuration.MaxBodyBytes)
|
|
if body.Error != nil {
|
|
Response_Error(writer, int(400), StdErrorsGo_Message(body.Error))
|
|
return
|
|
}
|
|
var revocation DeviceRevocationRequest = DeviceRevocationRequest{}
|
|
var value_0 Error = StdJsonGo_Unmarshal(body.Value, &(revocation))
|
|
if value_0 != nil {
|
|
Response_Error(writer, int(400), "invalid device revocation")
|
|
return
|
|
}
|
|
DeviceKeys_NormalizeRevocation(&(revocation))
|
|
context := StdHttpGo_Context(request)
|
|
verified := DeviceKeys_VerifyRevocation(devices.Database, context, accountID, revocation, devices.Verify)
|
|
if verified.Error != nil || verified.Status != int(0) {
|
|
HttpAuth_Respond(writer, devices.Counters, verified)
|
|
return
|
|
}
|
|
error := DeviceKeys_Revoke(devices.Database, context, accountID, revocation, devices.ReplayError)
|
|
if error != nil {
|
|
if StdErrorsGo_Is(error, devices.ReplayError) {
|
|
Response_Error(writer, int(409), "device revocation replay")
|
|
return
|
|
}
|
|
Response_Error(writer, int(500), "device revocation failed")
|
|
return
|
|
}
|
|
var value DeviceRevocationResponse = DeviceRevocationResponse{}
|
|
value.Status = "revoked"
|
|
Response_JSON(writer, int(200), value)
|
|
}
|