{"action":"created","comment":{"url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/comments/65388636","id":65388636,"diff_hunk":"@@ -0,0 +1,124 @@\n+// Copyright 2016 Google Inc. All Rights Reserved.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a copy of the License at\n+//\n+//  http://www.apache.org/licenses/LICENSE-2.0\n+//\n+// Unless required by applicable law or agreed to in writing, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+// See the License for the specific language governing permissions and\n+// limitations under the License.\n+\n+package webpush\n+\n+import (\n+\t\"crypto\"\n+\t\"crypto/ecdsa\"\n+\t\"crypto/elliptic\"\n+\t\"crypto/rand\"\n+\t\"encoding/base64\"\n+\t\"encoding/json\"\n+\t\"math/big\"\n+\t\"net/url\"\n+\t\"time\"\n+)\n+\n+var (\n+\tvapidPrefix = []byte(\"eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.\")\n+\tdot         = []byte(\".\")\n+)\n+\n+type jwtPrefix struct {\n+}\n+\n+type jwtBody struct {\n+\tAud string `json:\"aud\"`\n+\tSub string `json:\"sub,omitempty\"`\n+\tExp int64  `json:\"exp\"`\n+}\n+\n+// Vapid represents a sender identity.\n+type Vapid struct {\n+\t// The EC256 public key, base64 urlsafe, uncompressed. This value should be\n+\t// used in 'subscribe' requests and is included as p256ecdsa in\n+\t// the Crypto-Key header.\n+\tPublicKey string\n+\n+\t// The private key used to sign tokens\n+\tpkey *ecdsa.PrivateKey\n+\n+\t// Sub should be an email or URL, for identification\n+\tSub string\n+}\n+\n+// TODO: cache for tokens\n+\n+// Token creates a token with the specified endpoint, using configured Sub id\n+// and a default expiration (1h).\n+func (vapid *Vapid) Token(aud string) (res string) {\n+\turl, _ := url.Parse(aud)\n+\thost := url.Host\n+\tjwt := jwtBody{Aud: \"https://\" + host}\n+\tif vapid.Sub != \"\" {\n+\t\tjwt.Sub = vapid.Sub\n+\t}\n+\tjwt.Exp = int64(time.Now().Unix() + 3600)\n+\tt, _ := json.Marshal(jwt)\n+\tenc := base64.RawURLEncoding\n+\n+\tt64 := make([]byte, enc.EncodedLen(len(t)))\n+\tenc.Encode(t64, t)\n+\n+\ttoken := make([]byte, len(t)+len(vapidPrefix)+100)\n+\ttoken = append(token[:0], vapidPrefix...)\n+\ttoken = append(token, t64...)\n+\n+\thasher := crypto.SHA256.New()\n+\thasher.Write(token)\n+\n+\tif r, s, err := ecdsa.Sign(rand.Reader, vapid.pkey, hasher.Sum(nil)); err == nil {\n+\t\t// Vapid key is 32 bytes\n+\t\tkeyBytes := 32\n+\t\tsig := make([]byte, 2*keyBytes)\n+\n+\t\trBytes := r.Bytes()\n+\t\trBytesPadded := make([]byte, keyBytes)\n+\t\tcopy(rBytesPadded[keyBytes-len(rBytes):], rBytes)\n+\n+\t\tsBytes := s.Bytes()\n+\t\tsBytesPadded := make([]byte, keyBytes)\n+\t\tcopy(sBytesPadded[keyBytes-len(sBytes):], sBytes)\n+\n+\t\tsig = append(sig[:0], rBytesPadded...)\n+\t\tsig = append(sig, sBytesPadded...)\n+\n+\t\tsigB64 := make([]byte, enc.EncodedLen(len(sig)))\n+\t\tenc.Encode(sigB64, sig)\n+\n+\t\ttoken = append(token, dot...)\n+\t\ttoken = append(token, sigB64...)\n+\t}\n+\tres = string(token)\n+\treturn\n+}\n+\n+// NewVapid constructs a new Vapid generator from EC256 public and private keys,\n+// in uncompressed format.\n+func NewVapid(publicKey, privateKey string) (v *Vapid) {","path":"webpush/vapid.go","position":110,"original_position":110,"commit_id":"3c36d30c49ddeef56723fc7119ed63a9aa8af0e6","original_commit_id":"3c36d30c49ddeef56723fc7119ed63a9aa8af0e6","user":{"login":"costinm","id":84202,"avatar_url":"https://avatars.githubusercontent.com/u/84202?v=3","gravatar_id":"","url":"https://api.github.com/users/costinm","html_url":"https://github.com/costinm","followers_url":"https://api.github.com/users/costinm/followers","following_url":"https://api.github.com/users/costinm/following{/other_user}","gists_url":"https://api.github.com/users/costinm/gists{/gist_id}","starred_url":"https://api.github.com/users/costinm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/costinm/subscriptions","organizations_url":"https://api.github.com/users/costinm/orgs","repos_url":"https://api.github.com/users/costinm/repos","events_url":"https://api.github.com/users/costinm/events{/privacy}","received_events_url":"https://api.github.com/users/costinm/received_events","type":"User","site_admin":false},"body":"I tried that - I could add a method with the []byte, but it seems easier/more natural this way, most of the time the keys are passed around as b64 strings - in headers, etc. ","created_at":"2016-06-01T15:52:29Z","updated_at":"2016-06-01T15:52:29Z","html_url":"https://github.com/GoogleChrome/push-encryption-go/pull/11#discussion_r65388636","pull_request_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11","_links":{"self":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/comments/65388636"},"html":{"href":"https://github.com/GoogleChrome/push-encryption-go/pull/11#discussion_r65388636"},"pull_request":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11"}}},"pull_request":{"url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11","id":71418433,"html_url":"https://github.com/GoogleChrome/push-encryption-go/pull/11","diff_url":"https://github.com/GoogleChrome/push-encryption-go/pull/11.diff","patch_url":"https://github.com/GoogleChrome/push-encryption-go/pull/11.patch","issue_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/issues/11","number":11,"state":"open","locked":false,"title":"Add Vapid support and assorted fixes","user":{"login":"costinm","id":84202,"avatar_url":"https://avatars.githubusercontent.com/u/84202?v=3","gravatar_id":"","url":"https://api.github.com/users/costinm","html_url":"https://github.com/costinm","followers_url":"https://api.github.com/users/costinm/followers","following_url":"https://api.github.com/users/costinm/following{/other_user}","gists_url":"https://api.github.com/users/costinm/gists{/gist_id}","starred_url":"https://api.github.com/users/costinm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/costinm/subscriptions","organizations_url":"https://api.github.com/users/costinm/orgs","repos_url":"https://api.github.com/users/costinm/repos","events_url":"https://api.github.com/users/costinm/events{/privacy}","received_events_url":"https://api.github.com/users/costinm/received_events","type":"User","site_admin":false},"body":"The 'integration' tests are passing, but likely to fail when the subscriptions expire - \r\na more 'end-to-end' solution that includes registration is needed.","created_at":"2016-05-25T16:04:01Z","updated_at":"2016-06-01T15:52:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b33a868f0141b0a5dc90293ad5f871d9292ce86b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11/commits","review_comments_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11/comments","review_comment_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/comments{/number}","comments_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/issues/11/comments","statuses_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/statuses/3c36d30c49ddeef56723fc7119ed63a9aa8af0e6","head":{"label":"costinm:master","ref":"master","sha":"3c36d30c49ddeef56723fc7119ed63a9aa8af0e6","user":{"login":"costinm","id":84202,"avatar_url":"https://avatars.githubusercontent.com/u/84202?v=3","gravatar_id":"","url":"https://api.github.com/users/costinm","html_url":"https://github.com/costinm","followers_url":"https://api.github.com/users/costinm/followers","following_url":"https://api.github.com/users/costinm/following{/other_user}","gists_url":"https://api.github.com/users/costinm/gists{/gist_id}","starred_url":"https://api.github.com/users/costinm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/costinm/subscriptions","organizations_url":"https://api.github.com/users/costinm/orgs","repos_url":"https://api.github.com/users/costinm/repos","events_url":"https://api.github.com/users/costinm/events{/privacy}","received_events_url":"https://api.github.com/users/costinm/received_events","type":"User","site_admin":false},"repo":{"id":58674534,"name":"push-encryption-go","full_name":"costinm/push-encryption-go","owner":{"login":"costinm","id":84202,"avatar_url":"https://avatars.githubusercontent.com/u/84202?v=3","gravatar_id":"","url":"https://api.github.com/users/costinm","html_url":"https://github.com/costinm","followers_url":"https://api.github.com/users/costinm/followers","following_url":"https://api.github.com/users/costinm/following{/other_user}","gists_url":"https://api.github.com/users/costinm/gists{/gist_id}","starred_url":"https://api.github.com/users/costinm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/costinm/subscriptions","organizations_url":"https://api.github.com/users/costinm/orgs","repos_url":"https://api.github.com/users/costinm/repos","events_url":"https://api.github.com/users/costinm/events{/privacy}","received_events_url":"https://api.github.com/users/costinm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/costinm/push-encryption-go","description":"","fork":true,"url":"https://api.github.com/repos/costinm/push-encryption-go","forks_url":"https://api.github.com/repos/costinm/push-encryption-go/forks","keys_url":"https://api.github.com/repos/costinm/push-encryption-go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/costinm/push-encryption-go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/costinm/push-encryption-go/teams","hooks_url":"https://api.github.com/repos/costinm/push-encryption-go/hooks","issue_events_url":"https://api.github.com/repos/costinm/push-encryption-go/issues/events{/number}","events_url":"https://api.github.com/repos/costinm/push-encryption-go/events","assignees_url":"https://api.github.com/repos/costinm/push-encryption-go/assignees{/user}","branches_url":"https://api.github.com/repos/costinm/push-encryption-go/branches{/branch}","tags_url":"https://api.github.com/repos/costinm/push-encryption-go/tags","blobs_url":"https://api.github.com/repos/costinm/push-encryption-go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/costinm/push-encryption-go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/costinm/push-encryption-go/git/refs{/sha}","trees_url":"https://api.github.com/repos/costinm/push-encryption-go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/costinm/push-encryption-go/statuses/{sha}","languages_url":"https://api.github.com/repos/costinm/push-encryption-go/languages","stargazers_url":"https://api.github.com/repos/costinm/push-encryption-go/stargazers","contributors_url":"https://api.github.com/repos/costinm/push-encryption-go/contributors","subscribers_url":"https://api.github.com/repos/costinm/push-encryption-go/subscribers","subscription_url":"https://api.github.com/repos/costinm/push-encryption-go/subscription","commits_url":"https://api.github.com/repos/costinm/push-encryption-go/commits{/sha}","git_commits_url":"https://api.github.com/repos/costinm/push-encryption-go/git/commits{/sha}","comments_url":"https://api.github.com/repos/costinm/push-encryption-go/comments{/number}","issue_comment_url":"https://api.github.com/repos/costinm/push-encryption-go/issues/comments{/number}","contents_url":"https://api.github.com/repos/costinm/push-encryption-go/contents/{+path}","compare_url":"https://api.github.com/repos/costinm/push-encryption-go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/costinm/push-encryption-go/merges","archive_url":"https://api.github.com/repos/costinm/push-encryption-go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/costinm/push-encryption-go/downloads","issues_url":"https://api.github.com/repos/costinm/push-encryption-go/issues{/number}","pulls_url":"https://api.github.com/repos/costinm/push-encryption-go/pulls{/number}","milestones_url":"https://api.github.com/repos/costinm/push-encryption-go/milestones{/number}","notifications_url":"https://api.github.com/repos/costinm/push-encryption-go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/costinm/push-encryption-go/labels{/name}","releases_url":"https://api.github.com/repos/costinm/push-encryption-go/releases{/id}","deployments_url":"https://api.github.com/repos/costinm/push-encryption-go/deployments","created_at":"2016-05-12T20:28:52Z","updated_at":"2016-05-17T16:34:11Z","pushed_at":"2016-05-21T23:00:15Z","git_url":"git://github.com/costinm/push-encryption-go.git","ssh_url":"git@github.com:costinm/push-encryption-go.git","clone_url":"https://github.com/costinm/push-encryption-go.git","svn_url":"https://github.com/costinm/push-encryption-go","homepage":null,"size":39,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"GoogleChrome:master","ref":"master","sha":"f3ffca445dbd99a2dcfe25395f2d2b6b3e06268b","user":{"login":"GoogleChrome","id":1778935,"avatar_url":"https://avatars.githubusercontent.com/u/1778935?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleChrome","html_url":"https://github.com/GoogleChrome","followers_url":"https://api.github.com/users/GoogleChrome/followers","following_url":"https://api.github.com/users/GoogleChrome/following{/other_user}","gists_url":"https://api.github.com/users/GoogleChrome/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleChrome/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleChrome/subscriptions","organizations_url":"https://api.github.com/users/GoogleChrome/orgs","repos_url":"https://api.github.com/users/GoogleChrome/repos","events_url":"https://api.github.com/users/GoogleChrome/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleChrome/received_events","type":"Organization","site_admin":false},"repo":{"id":56687351,"name":"push-encryption-go","full_name":"GoogleChrome/push-encryption-go","owner":{"login":"GoogleChrome","id":1778935,"avatar_url":"https://avatars.githubusercontent.com/u/1778935?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleChrome","html_url":"https://github.com/GoogleChrome","followers_url":"https://api.github.com/users/GoogleChrome/followers","following_url":"https://api.github.com/users/GoogleChrome/following{/other_user}","gists_url":"https://api.github.com/users/GoogleChrome/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleChrome/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleChrome/subscriptions","organizations_url":"https://api.github.com/users/GoogleChrome/orgs","repos_url":"https://api.github.com/users/GoogleChrome/repos","events_url":"https://api.github.com/users/GoogleChrome/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleChrome/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/GoogleChrome/push-encryption-go","description":"","fork":false,"url":"https://api.github.com/repos/GoogleChrome/push-encryption-go","forks_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/forks","keys_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/teams","hooks_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/hooks","issue_events_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/issues/events{/number}","events_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/events","assignees_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/assignees{/user}","branches_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/branches{/branch}","tags_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/tags","blobs_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/git/refs{/sha}","trees_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/statuses/{sha}","languages_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/languages","stargazers_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/stargazers","contributors_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/contributors","subscribers_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/subscribers","subscription_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/subscription","commits_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/commits{/sha}","git_commits_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/git/commits{/sha}","comments_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/comments{/number}","issue_comment_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/issues/comments{/number}","contents_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/contents/{+path}","compare_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/merges","archive_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/downloads","issues_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/issues{/number}","pulls_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls{/number}","milestones_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/milestones{/number}","notifications_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/labels{/name}","releases_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/releases{/id}","deployments_url":"https://api.github.com/repos/GoogleChrome/push-encryption-go/deployments","created_at":"2016-04-20T13:04:09Z","updated_at":"2016-05-31T12:50:10Z","pushed_at":"2016-06-01T10:20:53Z","git_url":"git://github.com/GoogleChrome/push-encryption-go.git","ssh_url":"git@github.com:GoogleChrome/push-encryption-go.git","clone_url":"https://github.com/GoogleChrome/push-encryption-go.git","svn_url":"https://github.com/GoogleChrome/push-encryption-go","homepage":null,"size":18930,"stargazers_count":30,"watchers_count":30,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":4,"forks":3,"open_issues":4,"watchers":30,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11"},"html":{"href":"https://github.com/GoogleChrome/push-encryption-go/pull/11"},"issue":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/issues/11"},"comments":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/issues/11/comments"},"review_comments":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11/comments"},"review_comment":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/pulls/11/commits"},"statuses":{"href":"https://api.github.com/repos/GoogleChrome/push-encryption-go/statuses/3c36d30c49ddeef56723fc7119ed63a9aa8af0e6"}}}}
{"actor":{"display_login":"costinm"}}