-
Notifications
You must be signed in to change notification settings - Fork 29
/
jwt_action.go
67 lines (54 loc) · 1.38 KB
/
jwt_action.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
66
67
package provision
import (
"context"
"fmt"
"os"
"github.com/choria-io/go-choria/build"
"github.com/choria-io/go-choria/choria"
"github.com/choria-io/go-choria/inter"
"github.com/choria-io/go-choria/providers/agent/mcorpc"
)
type JWTRequest struct {
Token string `json:"token"`
}
type JWTReply struct {
JWT string `json:"jwt"`
ECDHPublic string `json:"ecdh_public"`
}
func jwtAction(ctx context.Context, req *mcorpc.Request, reply *mcorpc.Reply, agent *mcorpc.Agent, conn inter.ConnectorInfo) {
if !agent.Choria.ProvisionMode() {
abort("Cannot reconfigure a server that is not in provisioning mode", reply)
return
}
args := &JWTRequest{}
if !mcorpc.ParseRequestData(args, req, reply) {
return
}
if !checkToken(args.Token, reply) {
return
}
if build.ProvisionJWTFile == "" {
abort("No Provisioning JWT file has been configured", reply)
return
}
if !choria.FileExist(build.ProvisionJWTFile) {
abort("Provisioning JWT file does not exist", reply)
return
}
j, err := os.ReadFile(build.ProvisionJWTFile)
if err != nil {
abort(fmt.Sprintf("Could not read Provisioning JWT: %s", err), reply)
return
}
mu.Lock()
defer mu.Unlock()
err = updateECDHLocked()
if err != nil {
abort(fmt.Sprintf("Could not calculate EDCH keys: %s", err), reply)
return
}
reply.Data = JWTReply{
JWT: string(j),
ECDHPublic: fmt.Sprintf("%x", ecdhPublic),
}
}