-
Notifications
You must be signed in to change notification settings - Fork 7
/
validate.go
47 lines (37 loc) · 1.14 KB
/
validate.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
package devicecheck
import (
"fmt"
"net/http"
"time"
"github.com/google/uuid"
)
const validateDeviceTokenPath = "/validate_device_token"
type validateDeviceTokenRequestBody struct {
DeviceToken string `json:"device_token"`
TransactionID string `json:"transaction_id"`
Timestamp int64 `json:"timestamp"`
}
// ValidateDeviceToken validates a device for device token.
func (client *Client) ValidateDeviceToken(deviceToken string) error {
key, err := client.cred.key()
if err != nil {
return fmt.Errorf("devicecheck: failed to create key: %w", err)
}
jwt, err := client.jwt.generate(key)
if err != nil {
return fmt.Errorf("devicecheck: failed to generate jwt: %w", err)
}
body := validateDeviceTokenRequestBody{
DeviceToken: deviceToken,
TransactionID: uuid.New().String(),
Timestamp: time.Now().UTC().UnixNano() / int64(time.Millisecond),
}
code, respBody, err := client.api.do(jwt, validateDeviceTokenPath, body)
if err != nil {
return fmt.Errorf("devicecheck: failed to validate device token: %w", err)
}
if code != http.StatusOK {
return fmt.Errorf("devicecheck: %w", newError(code, respBody))
}
return nil
}