-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
signUp.go
65 lines (56 loc) · 1.92 KB
/
signUp.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
63
64
65
package service
import (
"fmt"
coapCodes "github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/mux"
"github.com/plgd-dev/hub/v2/coap-gateway/coapconv"
coapgwService "github.com/plgd-dev/hub/v2/coap-gateway/service"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/kit/v2/codec/cbor"
)
// https://github.com/openconnectivityfoundation/security/blob/master/swagger2.0/oic.sec.account.swagger.json
func signUpPostHandler(r *mux.Message, client *Client) {
logErrorAndCloseClient := func(err error, code coapCodes.Code) {
client.logAndWriteErrorResponse(fmt.Errorf("cannot handle sign up: %w", err), code, r.Token())
if client.handler == nil || client.handler.CloseOnError() {
if err := client.Close(); err != nil {
log.Errorf("sign up error: %w", err)
}
}
}
var signUp coapgwService.CoapSignUpRequest
if err := cbor.ReadFrom(r.Body(), &signUp); err != nil {
logErrorAndCloseClient(err, coapCodes.BadRequest)
return
}
client.SetDeviceID(signUp.DeviceID)
resp, err := client.handler.SignUp(signUp)
if err != nil {
logErrorAndCloseClient(err, coapCodes.InternalServerError)
return
}
accept := coapconv.GetAccept(r.Options())
encode, err := coapconv.GetEncoder(accept)
if err != nil {
logErrorAndCloseClient(err, coapCodes.InternalServerError)
return
}
out, err := encode(resp)
if err != nil {
logErrorAndCloseClient(err, coapCodes.InternalServerError)
return
}
client.sendResponse(coapCodes.Changed, r.Token(), accept, out)
}
// Sign-up
// https://github.com/openconnectivityfoundation/security/blob/master/swagger2.0/oic.sec.account.swagger.json
func signUpHandler(r *mux.Message, client *Client) {
switch r.Code() {
case coapCodes.POST:
signUpPostHandler(r, client)
case coapCodes.DELETE:
signOffHandler(r, client)
default:
client.logAndWriteErrorResponse(fmt.Errorf("forbidden request from %v", client.RemoteAddrString()), coapCodes.Forbidden, r.Token())
}
}