-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
56 lines (50 loc) · 1.47 KB
/
auth.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
package main
import (
pb "github.com/opencopilot/core/core"
"github.com/opencopilot/core/instance"
packet "github.com/packethost/packngo"
)
// PacketAuth implements auth for Packet instances
type PacketAuth struct {
*pb.Auth
}
// Verify that a given Auth payload has access to a Packet account
func (p *PacketAuth) Verify() bool {
projectID, err := GetPacketProjectFromAuthPayload(p.Payload)
if err != nil {
return false
}
return projectID != ""
}
// CanManageInstance verifies that the passed in authentication can manage the specified instance, if it's a Packet instance
func (p *PacketAuth) CanManageInstance(instance *instance.Instance) bool {
client := packet.NewClientWithAuth("", p.Payload, nil)
device, _, err := client.Devices.Get(instance.Device)
if err != nil {
return false
}
if device != nil {
return true
}
return false
}
// VerifyAuthentication verifies that a given Auth payload can authenticate to the provider it specifies
func VerifyAuthentication(auth *pb.Auth) bool {
switch auth.Provider {
case pb.Provider_PACKET:
authProvider := PacketAuth{auth}
return authProvider.Verify()
default:
return false
}
}
// CanManageInstance checks whether or not the passed in authentication can manage the specified instance
func CanManageInstance(auth *pb.Auth, instance *instance.Instance) bool {
switch auth.Provider {
case pb.Provider_PACKET:
authProvider := PacketAuth{auth}
return authProvider.CanManageInstance(instance)
default:
return false
}
}