-
Notifications
You must be signed in to change notification settings - Fork 4
/
verifier.go
143 lines (117 loc) · 4.26 KB
/
verifier.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package android
import (
"context"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
"google.golang.org/api/playintegrity/v1"
"github.com/code-payments/code-server/pkg/device"
"github.com/code-payments/code-server/pkg/metrics"
)
const (
metricsStructName = "device.android.verifier"
)
type androidDeviceVerifier struct {
playIntegrity *playintegrity.Service
packageName string
minVersion int64
}
// NewAndroidDeviceVerifier returns a new device.Verifier for Android devices
//
// Requires GOOGLE_APPLICATION_CREDENTIALS to be set
func NewAndroidDeviceVerifier(packageName string, minVersion int64) (device.Verifier, error) {
ctx := context.Background()
playIntegrityService, err := playintegrity.NewService(ctx)
if err != nil {
return nil, err
}
return &androidDeviceVerifier{
playIntegrity: playIntegrityService,
packageName: packageName,
minVersion: minVersion,
}, nil
}
// IsValid implements device.Verifier.IsValid
func (v *androidDeviceVerifier) IsValid(ctx context.Context, token string) (bool, string, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "IsValid")
defer tracer.End()
isValid, reason, err := func() (bool, string, error) {
resp, err := v.playIntegrity.V1.DecodeIntegrityToken(v.packageName, &playintegrity.DecodeIntegrityTokenRequest{
IntegrityToken: token,
}).Context(ctx).Do()
if err != nil {
return false, "", err
}
if resp.ServerResponse.HTTPStatusCode != http.StatusOK {
return false, "", errors.Errorf("received http status %d", resp.ServerResponse.HTTPStatusCode)
}
requestedAt := time.UnixMilli(resp.TokenPayloadExternal.RequestDetails.TimestampMillis)
if time.Since(requestedAt) > time.Minute {
return false, "device token is too old", nil
}
if resp.TokenPayloadExternal.AccountDetails == nil {
return false, "account details missing", nil
}
if resp.TokenPayloadExternal.AppIntegrity == nil {
return false, "app integrity missing", nil
}
if resp.TokenPayloadExternal.DeviceIntegrity == nil {
return false, "device integrity missing", nil
}
if resp.TokenPayloadExternal.AppIntegrity.AppRecognitionVerdict != "PLAY_RECOGNIZED" {
return false, fmt.Sprintf("app recognition verdict is %s", resp.TokenPayloadExternal.AppIntegrity.AppRecognitionVerdict), nil
}
if resp.TokenPayloadExternal.AccountDetails.AppLicensingVerdict != "LICENSED" {
return false, fmt.Sprintf("app licensing verdict is %s", resp.TokenPayloadExternal.AccountDetails.AppLicensingVerdict), nil
}
if len(resp.TokenPayloadExternal.DeviceIntegrity.DeviceRecognitionVerdict) == 0 {
return false, "no device recognition verdicts", nil
}
for _, deviceRecognitionVerdict := range resp.TokenPayloadExternal.DeviceIntegrity.DeviceRecognitionVerdict {
switch deviceRecognitionVerdict {
case "MEETS_VIRTUAL_INTEGRITY", "UNKNOWN":
return false, fmt.Sprintf("device recognition verdict is %s", deviceRecognitionVerdict), nil
}
}
if resp.TokenPayloadExternal.AppIntegrity.PackageName != v.packageName {
return false, fmt.Sprintf("package name is %s", resp.TokenPayloadExternal.AppIntegrity.PackageName), nil
}
if resp.TokenPayloadExternal.AppIntegrity.VersionCode < v.minVersion {
return false, "minimum client version not met", nil
}
return true, "", nil
}()
if err != nil {
tracer.OnError(err)
}
return isValid, reason, err
}
// HasCreatedFreeAccount implements device.Verifier.HasCreatedFreeAccount
//
// todo: Currently impossible to implement without a stable device ID
func (v *androidDeviceVerifier) HasCreatedFreeAccount(ctx context.Context, token string) (bool, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "HasCreatedFreeAccount")
defer tracer.End()
hasFreeCreatedAccount, err := func() (bool, error) {
return false, nil
}()
if err != nil {
tracer.OnError(err)
}
return hasFreeCreatedAccount, err
}
// MarkCreatedFreeAccount implements device.Verifier.MarkCreatedFreeAccount
//
// todo: Currently impossible to implement without a stable device ID
func (v *androidDeviceVerifier) MarkCreatedFreeAccount(ctx context.Context, token string) error {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "MarkCreatedFreeAccount")
defer tracer.End()
err := func() error {
return nil
}()
if err != nil {
tracer.OnError(err)
}
return err
}