-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f770df9
commit 4ed12c2
Showing
5 changed files
with
169 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package googleiid | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/wearemojo/mojo-public-go/lib/errgroup" | ||
"github.com/wearemojo/mojo-public-go/lib/httpclient" | ||
"github.com/wearemojo/mojo-public-go/lib/jsonclient" | ||
"github.com/wearemojo/mojo-public-go/lib/secret" | ||
) | ||
|
||
const baseURL = "https://iid.googleapis.com" | ||
|
||
type Client struct { | ||
client *jsonclient.Client | ||
} | ||
|
||
func NewClient(ctx context.Context, serverKeySecretID, vapidPublicKeySecretID string) (*Client, error) { | ||
g := errgroup.WithContext(ctx) | ||
|
||
g.Go(func(ctx context.Context) (err error) { | ||
_, err = secret.Get(ctx, serverKeySecretID) | ||
return | ||
}) | ||
|
||
g.Go(func(ctx context.Context) (err error) { | ||
_, err = secret.Get(ctx, vapidPublicKeySecretID) | ||
return | ||
}) | ||
|
||
if err := g.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{ | ||
client: jsonclient.NewClient( | ||
baseURL, | ||
httpclient.NewClient(5*time.Second, roundTripper{ | ||
ServerKeySecretID: serverKeySecretID, | ||
VAPIDPublicKeySecretID: vapidPublicKeySecretID, | ||
}), | ||
), | ||
}, nil | ||
} | ||
|
||
type APNSRequest struct { | ||
Application string `json:"application"` | ||
Sandbox bool `json:"sandbox"` | ||
APNSTokens []string `json:"apns_tokens"` | ||
} | ||
|
||
type APNSResponse struct { | ||
// order does not relate to the request ordering - match by token | ||
Results []APNSResponseResult `json:"results"` | ||
} | ||
|
||
type APNSResponseResult struct { | ||
RegistrationToken string `json:"registration_token"` | ||
APNSToken string `json:"apns_token"` | ||
Status string `json:"status"` | ||
} | ||
|
||
func (r APNSResponseResult) Valid() bool { | ||
// we've also noticed `INVALID_ARGUMENT` and `INTERNAL` on the status field | ||
return r.Status == "OK" && r.APNSToken != "" && r.RegistrationToken != "" | ||
} | ||
|
||
type WebPushRequest struct { | ||
Endpoint string `json:"endpoint"` | ||
|
||
Keys WebPushRequestKeys `json:"keys"` | ||
} | ||
|
||
type WebPushRequestKeys struct { | ||
Auth string `json:"auth"` | ||
P256DH string `json:"p256dh"` | ||
} | ||
|
||
type WebPushResponse struct { | ||
Token string `json:"token"` | ||
} | ||
|
||
func (c *Client) ImportAPNSTokens(ctx context.Context, req *APNSRequest) (res *APNSResponse, err error) { | ||
// https://web.archive.org/web/20220407013020/https://developers.google.com/instance-id/reference/server#create_registration_tokens_for_apns_tokens | ||
return res, c.client.Do(ctx, "POST", "iid/v1:batchImport", nil, req, &res) | ||
} | ||
|
||
func (c *Client) ImportWebPushSubscription(ctx context.Context, req *WebPushRequest) (res *WebPushResponse, err error) { | ||
// https://web.archive.org/web/20220407013020/https://developers.google.com/instance-id/reference/server#import_push_subscriptions | ||
return res, c.client.Do(ctx, "POST", "v1/web/iid", nil, req, &res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package googleiid | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/wearemojo/mojo-public-go/lib/errgroup" | ||
"github.com/wearemojo/mojo-public-go/lib/secret" | ||
) | ||
|
||
type roundTripper struct { | ||
ServerKeySecretID string | ||
VAPIDPublicKeySecretID string | ||
} | ||
|
||
func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
ctx := req.Context() | ||
req = req.Clone(ctx) | ||
|
||
var serverKey string | ||
var vapidPublicKey string | ||
|
||
g := errgroup.WithContext(ctx) | ||
|
||
g.Go(func(ctx context.Context) (err error) { | ||
serverKey, err = secret.Get(ctx, r.ServerKeySecretID) | ||
return | ||
}) | ||
|
||
g.Go(func(ctx context.Context) (err error) { | ||
vapidPublicKey, err = secret.Get(ctx, r.VAPIDPublicKeySecretID) | ||
return | ||
}) | ||
|
||
if err := g.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
if req.Header == nil { | ||
req.Header = http.Header{} | ||
} | ||
|
||
// https://web.archive.org/web/20221206045856/https://firebase.google.com/docs/cloud-messaging/auth-server#authorize-http-requests | ||
req.Header.Set("Authorization", "key="+serverKey) | ||
|
||
// https://developers.google.com/instance-id/reference/server#parameters_5 | ||
req.Header.Set("Crypto-Key", "p256ecdsa="+vapidPublicKey) | ||
|
||
return http.DefaultTransport.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters