{"action":"created","comment":{"url":"https://api.github.com/repos/keybase/client/pulls/comments/203555319","pull_request_review_id":138469634,"id":203555319,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwMzU1NTMxOQ==","diff_hunk":"@@ -0,0 +1,191 @@\n+// Copyright 2018 Keybase, Inc. All rights reserved. Use of\n+// this source code is governed by the included BSD license.\n+\n+package libkb\n+\n+import (\n+\t\"fmt\"\n+\n+\tjsonw \"github.com/keybase/go-jsonw\"\n+)\n+\n+const (\n+\tDefaultCloneTokenValue string = \"00000000000000000000000000000000\"\n+)\n+\n+type DeviceCloneState struct {\n+\tPrior  string\n+\tStage  string\n+\tClones int\n+}\n+\n+type DeviceCloneStateJSONFile struct {\n+\t*JSONFile\n+}\n+type cloneDetectionResponse struct {\n+\tStatus AppStatus `json:\"status\"`\n+\tToken  string    `json:\"token\"`\n+\tClones int       `json:\"clones\"`\n+}\n+\n+func (d *cloneDetectionResponse) GetAppStatus() *AppStatus {\n+\treturn &d.Status\n+}\n+\n+func UpdateDeviceCloneState(m MetaContext) (before int, after int, err error) {\n+\td, err := GetDeviceCloneState(m)\n+\tbefore = d.Clones\n+\n+\tprior, stage := d.Prior, d.Stage\n+\tif prior == \"\" {\n+\t\t//first run\n+\t\tprior = DefaultCloneTokenValue\n+\t}\n+\tif stage == \"\" {\n+\t\tstage, err = RandHexString(\"\", 16)\n+\t\tif err != nil {\n+\t\t\treturn 0, 0, err\n+\t\t}\n+\t\terr = SetDeviceCloneState(m, DeviceCloneState{Prior: prior, Stage: stage, Clones: d.Clones})\n+\t\tif err != nil {\n+\t\t\treturn 0, 0, err\n+\t\t}\n+\t}\n+\n+\t// POST these tokens to the server\n+\targ := APIArg{\n+\t\tEndpoint:    \"device/clone_detection_token\",\n+\t\tSessionType: APISessionTypeREQUIRED,\n+\t\tArgs: HTTPArgs{\n+\t\t\t\"device_id\": m.G().ActiveDevice.DeviceID(),\n+\t\t\t\"prior\":     S{Val: prior},\n+\t\t\t\"stage\":     S{Val: stage},\n+\t\t},\n+\t\tMetaContext:    m,\n+\t\tAppStatusCodes: []int{SCOk},\n+\t}\n+\tvar res cloneDetectionResponse\n+\terr = m.G().API.PostDecode(arg, &res)\n+\tif err != nil {\n+\t\treturn 0, 0, err\n+\t}\n+\n+\terr = SetDeviceCloneState(m, DeviceCloneState{Prior: res.Token, Stage: \"\", Clones: res.Clones})\n+\tafter = res.Clones\n+\treturn before, after, err\n+}\n+\n+func configPaths(m MetaContext) (string, string, string) {\n+\tdeviceID := m.G().ActiveDevice.DeviceID()\n+\tp := fmt.Sprintf(\"%s.prior\", deviceID)\n+\ts := fmt.Sprintf(\"%s.stage\", deviceID)\n+\tc := fmt.Sprintf(\"%s.clones\", deviceID)\n+\treturn p, s, c\n+}\n+\n+func GetDeviceCloneState(m MetaContext) (DeviceCloneState, error) {\n+\treader, err := newDeviceCloneStateReader(m)\n+\tpPath, sPath, cPath := configPaths(m)\n+\tp, _ := reader.GetStringAtPath(pPath)\n+\ts, _ := reader.GetStringAtPath(sPath)\n+\tc, _ := reader.GetIntAtPath(cPath)\n+\treturn DeviceCloneState{Prior: p, Stage: s, Clones: c}, err\n+}\n+\n+func SetDeviceCloneState(m MetaContext, d DeviceCloneState) error {\n+\twriter, err := newDeviceCloneStateWriter(m)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\ttx, err := writer.BeginTransaction()\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\t// From this point on, if there's an error, we abort the\n+\t// transaction.\n+\tdefer func() {\n+\t\tif tx != nil {\n+\t\t\ttx.Abort()\n+\t\t}\n+\t}()\n+\n+\tpPath, sPath, cPath := configPaths(m)\n+\terr = writer.SetStringAtPath(pPath, d.Prior)\n+\tif err == nil {\n+\t\terr = writer.SetStringAtPath(sPath, d.Stage)\n+\t}\n+\tif err == nil {\n+\t\terr = writer.SetIntAtPath(cPath, d.Clones)\n+\t}\n+\tif err == nil {\n+\t\terr = tx.Commit()\n+\t}\n+\tif err != nil {\n+\t\treturn err\n+\t}","path":"go/libkb/device_clone_state.go","position":128,"original_position":125,"commit_id":"cc55895bde4c520818f6f0ab3d3d22cfa384914a","original_commit_id":"103bcd7f98643657f672a42c91bee4c8fe2ddae9","user":{"login":"xgess","id":1275828,"node_id":"MDQ6VXNlcjEyNzU4Mjg=","avatar_url":"https://avatars2.githubusercontent.com/u/1275828?v=4","gravatar_id":"","url":"https://api.github.com/users/xgess","html_url":"https://github.com/xgess","followers_url":"https://api.github.com/users/xgess/followers","following_url":"https://api.github.com/users/xgess/following{/other_user}","gists_url":"https://api.github.com/users/xgess/gists{/gist_id}","starred_url":"https://api.github.com/users/xgess/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xgess/subscriptions","organizations_url":"https://api.github.com/users/xgess/orgs","repos_url":"https://api.github.com/users/xgess/repos","events_url":"https://api.github.com/users/xgess/events{/privacy}","received_events_url":"https://api.github.com/users/xgess/received_events","type":"User","site_admin":false},"body":"yyyeeeeaaahhhh. it's long and repetitive, but i guess it's the most obvious and go-y way to do it.","created_at":"2018-07-18T22:50:03Z","updated_at":"2018-07-18T22:50:03Z","html_url":"https://github.com/keybase/client/pull/12807#discussion_r203555319","pull_request_url":"https://api.github.com/repos/keybase/client/pulls/12807","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/keybase/client/pulls/comments/203555319"},"html":{"href":"https://github.com/keybase/client/pull/12807#discussion_r203555319"},"pull_request":{"href":"https://api.github.com/repos/keybase/client/pulls/12807"}},"in_reply_to_id":203535076},"pull_request":{"url":"https://api.github.com/repos/keybase/client/pulls/12807","id":201391563,"node_id":"MDExOlB1bGxSZXF1ZXN0MjAxMzkxNTYz","html_url":"https://github.com/keybase/client/pull/12807","diff_url":"https://github.com/keybase/client/pull/12807.diff","patch_url":"https://github.com/keybase/client/pull/12807.patch","issue_url":"https://api.github.com/repos/keybase/client/issues/12807","number":12807,"state":"open","locked":false,"title":"client side of device clone detection","user":{"login":"xgess","id":1275828,"node_id":"MDQ6VXNlcjEyNzU4Mjg=","avatar_url":"https://avatars2.githubusercontent.com/u/1275828?v=4","gravatar_id":"","url":"https://api.github.com/users/xgess","html_url":"https://github.com/xgess","followers_url":"https://api.github.com/users/xgess/followers","following_url":"https://api.github.com/users/xgess/following{/other_user}","gists_url":"https://api.github.com/users/xgess/gists{/gist_id}","starred_url":"https://api.github.com/users/xgess/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xgess/subscriptions","organizations_url":"https://api.github.com/users/xgess/orgs","repos_url":"https://api.github.com/users/xgess/repos","events_url":"https://api.github.com/users/xgess/events{/privacy}","received_events_url":"https://api.github.com/users/xgess/received_events","type":"User","site_admin":false},"body":"This is part of an effort to identify and notify about possible device clones. Periodically (currently every 6 hours) the client will send two tokens (`prior`, and `stage`) to the server, and based on its response, the client will decide whether or not to notify about a possible clone. Here's what the token interaction looks like from the client's perspective:\r\n1. Read [`prior`, `stage`] off persistent disk\r\n2. If `stage` is `\"\"` generate a new token for `stage` and write [`prior`, `stage`] to disk.\r\n3. Post [`prior`, `stage`] to the server. \r\n4. In the happy path, the server will update the token it's saving for this device from `prior` to `stage`. If there's a clone, we'll see one `prior` become multiple `stage`s. \r\n5. The client then updates it's persistent disk from [`prior`, `stage`] to [`stage`, `\"\"`] and sleeps until the next run.\r\n\r\nThis PR includes all of the token interaction/persistence and an RPC notification to the frontend. ","created_at":"2018-07-13T20:22:50Z","updated_at":"2018-07-18T22:50:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5e29e01fde25a0a3702898fc1b2f1c8f6920a498","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/keybase/client/pulls/12807/commits","review_comments_url":"https://api.github.com/repos/keybase/client/pulls/12807/comments","review_comment_url":"https://api.github.com/repos/keybase/client/pulls/comments{/number}","comments_url":"https://api.github.com/repos/keybase/client/issues/12807/comments","statuses_url":"https://api.github.com/repos/keybase/client/statuses/cc55895bde4c520818f6f0ab3d3d22cfa384914a","head":{"label":"keybase:xgess/CORE-8288-device-clone-detection","ref":"xgess/CORE-8288-device-clone-detection","sha":"cc55895bde4c520818f6f0ab3d3d22cfa384914a","user":{"login":"keybase","id":5400834,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0MDA4MzQ=","avatar_url":"https://avatars2.githubusercontent.com/u/5400834?v=4","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"repo":{"id":29099181,"node_id":"MDEwOlJlcG9zaXRvcnkyOTA5OTE4MQ==","name":"client","full_name":"keybase/client","owner":{"login":"keybase","id":5400834,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0MDA4MzQ=","avatar_url":"https://avatars2.githubusercontent.com/u/5400834?v=4","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/keybase/client","description":"Keybase Go Library, Client, Service, OS X, iOS, Android, Electron","fork":false,"url":"https://api.github.com/repos/keybase/client","forks_url":"https://api.github.com/repos/keybase/client/forks","keys_url":"https://api.github.com/repos/keybase/client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/keybase/client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/keybase/client/teams","hooks_url":"https://api.github.com/repos/keybase/client/hooks","issue_events_url":"https://api.github.com/repos/keybase/client/issues/events{/number}","events_url":"https://api.github.com/repos/keybase/client/events","assignees_url":"https://api.github.com/repos/keybase/client/assignees{/user}","branches_url":"https://api.github.com/repos/keybase/client/branches{/branch}","tags_url":"https://api.github.com/repos/keybase/client/tags","blobs_url":"https://api.github.com/repos/keybase/client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/keybase/client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/keybase/client/git/refs{/sha}","trees_url":"https://api.github.com/repos/keybase/client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/keybase/client/statuses/{sha}","languages_url":"https://api.github.com/repos/keybase/client/languages","stargazers_url":"https://api.github.com/repos/keybase/client/stargazers","contributors_url":"https://api.github.com/repos/keybase/client/contributors","subscribers_url":"https://api.github.com/repos/keybase/client/subscribers","subscription_url":"https://api.github.com/repos/keybase/client/subscription","commits_url":"https://api.github.com/repos/keybase/client/commits{/sha}","git_commits_url":"https://api.github.com/repos/keybase/client/git/commits{/sha}","comments_url":"https://api.github.com/repos/keybase/client/comments{/number}","issue_comment_url":"https://api.github.com/repos/keybase/client/issues/comments{/number}","contents_url":"https://api.github.com/repos/keybase/client/contents/{+path}","compare_url":"https://api.github.com/repos/keybase/client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/keybase/client/merges","archive_url":"https://api.github.com/repos/keybase/client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/keybase/client/downloads","issues_url":"https://api.github.com/repos/keybase/client/issues{/number}","pulls_url":"https://api.github.com/repos/keybase/client/pulls{/number}","milestones_url":"https://api.github.com/repos/keybase/client/milestones{/number}","notifications_url":"https://api.github.com/repos/keybase/client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/keybase/client/labels{/name}","releases_url":"https://api.github.com/repos/keybase/client/releases{/id}","deployments_url":"https://api.github.com/repos/keybase/client/deployments","created_at":"2015-01-11T17:17:12Z","updated_at":"2018-07-18T22:27:43Z","pushed_at":"2018-07-18T22:45:12Z","git_url":"git://github.com/keybase/client.git","ssh_url":"git@github.com:keybase/client.git","clone_url":"https://github.com/keybase/client.git","svn_url":"https://github.com/keybase/client","homepage":"","size":247364,"stargazers_count":3860,"watchers_count":3860,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":374,"mirror_url":null,"archived":false,"open_issues_count":2224,"license":{"key":"bsd-3-clause","name":"BSD 3-Clause \"New\" or \"Revised\" License","spdx_id":"BSD-3-Clause","url":"https://api.github.com/licenses/bsd-3-clause","node_id":"MDc6TGljZW5zZTU="},"forks":374,"open_issues":2224,"watchers":3860,"default_branch":"master"}},"base":{"label":"keybase:master","ref":"master","sha":"84cf1f343e0e8fb8d26c1826cb7680cb4dc6aafb","user":{"login":"keybase","id":5400834,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0MDA4MzQ=","avatar_url":"https://avatars2.githubusercontent.com/u/5400834?v=4","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"repo":{"id":29099181,"node_id":"MDEwOlJlcG9zaXRvcnkyOTA5OTE4MQ==","name":"client","full_name":"keybase/client","owner":{"login":"keybase","id":5400834,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0MDA4MzQ=","avatar_url":"https://avatars2.githubusercontent.com/u/5400834?v=4","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/keybase/client","description":"Keybase Go Library, Client, Service, OS X, iOS, Android, Electron","fork":false,"url":"https://api.github.com/repos/keybase/client","forks_url":"https://api.github.com/repos/keybase/client/forks","keys_url":"https://api.github.com/repos/keybase/client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/keybase/client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/keybase/client/teams","hooks_url":"https://api.github.com/repos/keybase/client/hooks","issue_events_url":"https://api.github.com/repos/keybase/client/issues/events{/number}","events_url":"https://api.github.com/repos/keybase/client/events","assignees_url":"https://api.github.com/repos/keybase/client/assignees{/user}","branches_url":"https://api.github.com/repos/keybase/client/branches{/branch}","tags_url":"https://api.github.com/repos/keybase/client/tags","blobs_url":"https://api.github.com/repos/keybase/client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/keybase/client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/keybase/client/git/refs{/sha}","trees_url":"https://api.github.com/repos/keybase/client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/keybase/client/statuses/{sha}","languages_url":"https://api.github.com/repos/keybase/client/languages","stargazers_url":"https://api.github.com/repos/keybase/client/stargazers","contributors_url":"https://api.github.com/repos/keybase/client/contributors","subscribers_url":"https://api.github.com/repos/keybase/client/subscribers","subscription_url":"https://api.github.com/repos/keybase/client/subscription","commits_url":"https://api.github.com/repos/keybase/client/commits{/sha}","git_commits_url":"https://api.github.com/repos/keybase/client/git/commits{/sha}","comments_url":"https://api.github.com/repos/keybase/client/comments{/number}","issue_comment_url":"https://api.github.com/repos/keybase/client/issues/comments{/number}","contents_url":"https://api.github.com/repos/keybase/client/contents/{+path}","compare_url":"https://api.github.com/repos/keybase/client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/keybase/client/merges","archive_url":"https://api.github.com/repos/keybase/client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/keybase/client/downloads","issues_url":"https://api.github.com/repos/keybase/client/issues{/number}","pulls_url":"https://api.github.com/repos/keybase/client/pulls{/number}","milestones_url":"https://api.github.com/repos/keybase/client/milestones{/number}","notifications_url":"https://api.github.com/repos/keybase/client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/keybase/client/labels{/name}","releases_url":"https://api.github.com/repos/keybase/client/releases{/id}","deployments_url":"https://api.github.com/repos/keybase/client/deployments","created_at":"2015-01-11T17:17:12Z","updated_at":"2018-07-18T22:27:43Z","pushed_at":"2018-07-18T22:45:12Z","git_url":"git://github.com/keybase/client.git","ssh_url":"git@github.com:keybase/client.git","clone_url":"https://github.com/keybase/client.git","svn_url":"https://github.com/keybase/client","homepage":"","size":247364,"stargazers_count":3860,"watchers_count":3860,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":374,"mirror_url":null,"archived":false,"open_issues_count":2224,"license":{"key":"bsd-3-clause","name":"BSD 3-Clause \"New\" or \"Revised\" License","spdx_id":"BSD-3-Clause","url":"https://api.github.com/licenses/bsd-3-clause","node_id":"MDc6TGljZW5zZTU="},"forks":374,"open_issues":2224,"watchers":3860,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/keybase/client/pulls/12807"},"html":{"href":"https://github.com/keybase/client/pull/12807"},"issue":{"href":"https://api.github.com/repos/keybase/client/issues/12807"},"comments":{"href":"https://api.github.com/repos/keybase/client/issues/12807/comments"},"review_comments":{"href":"https://api.github.com/repos/keybase/client/pulls/12807/comments"},"review_comment":{"href":"https://api.github.com/repos/keybase/client/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/keybase/client/pulls/12807/commits"},"statuses":{"href":"https://api.github.com/repos/keybase/client/statuses/cc55895bde4c520818f6f0ab3d3d22cfa384914a"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"xgess"}}