/go/libkb/proof_support_github.go

https://gitlab.com/yenny.prathivi/client · Go · 129 lines · 90 code · 29 blank · 10 comment · 11 complexity · 3d55a73fef2e79aa1f28ed8fbae7983b MD5 · raw file

  1. // Copyright 2015 Keybase, Inc. All rights reserved. Use of
  2. // this source code is governed by the included BSD license.
  3. package libkb
  4. import (
  5. "regexp"
  6. "strings"
  7. keybase1 "github.com/keybase/client/go/protocol"
  8. jsonw "github.com/keybase/go-jsonw"
  9. )
  10. //=============================================================================
  11. // Github
  12. //
  13. type GithubChecker struct {
  14. proof RemoteProofChainLink
  15. }
  16. func NewGithubChecker(p RemoteProofChainLink) (*GithubChecker, ProofError) {
  17. return &GithubChecker{p}, nil
  18. }
  19. func (rc *GithubChecker) GetTorError() ProofError { return nil }
  20. func (rc *GithubChecker) CheckHint(g *GlobalContext, h SigHint) ProofError {
  21. given := strings.ToLower(h.apiURL)
  22. u := strings.ToLower(rc.proof.GetRemoteUsername())
  23. ok1 := "https://gist.github.com/" + u + "/"
  24. ok2 := "https://gist.githubusercontent.com/" + u + "/"
  25. if strings.HasPrefix(given, ok1) || strings.HasPrefix(given, ok2) {
  26. return nil
  27. }
  28. return NewProofError(keybase1.ProofStatus_BAD_API_URL,
  29. "Bad hint from server; URL start with either '%s' OR '%s'", ok1, ok2)
  30. }
  31. func (rc *GithubChecker) CheckStatus(g *GlobalContext, h SigHint) ProofError {
  32. res, err := g.XAPI.GetText(APIArg{
  33. Endpoint: h.apiURL,
  34. NeedSession: false,
  35. })
  36. if err != nil {
  37. return XapiError(err, h.apiURL)
  38. }
  39. var sigBody []byte
  40. sigBody, _, err = OpenSig(rc.proof.GetArmoredSig())
  41. var ret ProofError
  42. if err != nil {
  43. return NewProofError(keybase1.ProofStatus_BAD_SIGNATURE,
  44. "Bad signature: %s", err)
  45. }
  46. if !FindBase64Block(res.Body, sigBody, false) {
  47. ret = NewProofError(keybase1.ProofStatus_TEXT_NOT_FOUND, "signature not found in body")
  48. }
  49. return ret
  50. }
  51. //
  52. //=============================================================================
  53. type GithubServiceType struct{ BaseServiceType }
  54. func (t GithubServiceType) AllStringKeys() []string { return t.BaseAllStringKeys(t) }
  55. var githubUsernameRegexp = regexp.MustCompile(`^(?i:[a-z0-9][a-z0-9-]{0,38})$`)
  56. func (t GithubServiceType) NormalizeUsername(s string) (string, error) {
  57. if !githubUsernameRegexp.MatchString(s) {
  58. return "", BadUsernameError{s}
  59. }
  60. return strings.ToLower(s), nil
  61. }
  62. func (t GithubServiceType) NormalizeRemoteName(g *GlobalContext, s string) (ret string, err error) {
  63. // Allow a leading '@'.
  64. s = strings.TrimPrefix(s, "@")
  65. return t.NormalizeUsername(s)
  66. }
  67. func (t GithubServiceType) GetPrompt() string {
  68. return "Your username on Github"
  69. }
  70. func (t GithubServiceType) ToServiceJSON(un string) *jsonw.Wrapper {
  71. return t.BaseToServiceJSON(t, un)
  72. }
  73. func (t GithubServiceType) PostInstructions(un string) *Markup {
  74. return FmtMarkup(`Please <strong>publicly</strong> post the following Gist,
  75. and name it <strong><color name="red">keybase.md</color></strong>`)
  76. }
  77. func (t GithubServiceType) DisplayName(un string) string { return "Github" }
  78. func (t GithubServiceType) GetTypeName() string { return "github" }
  79. func (t GithubServiceType) RecheckProofPosting(tryNumber int, status keybase1.ProofStatus, _ string) (warning *Markup, err error) {
  80. if status == keybase1.ProofStatus_PERMISSION_DENIED {
  81. warning = FmtMarkup("Permission denied! Make sure your gist is <strong>public</strong>.")
  82. } else {
  83. warning, err = t.BaseRecheckProofPosting(tryNumber, status)
  84. }
  85. return
  86. }
  87. func (t GithubServiceType) GetProofType() string { return t.BaseGetProofType(t) }
  88. func (t GithubServiceType) CheckProofText(text string, id keybase1.SigID, sig string) (err error) {
  89. return t.BaseCheckProofTextFull(text, id, sig)
  90. }
  91. //=============================================================================
  92. func init() {
  93. RegisterServiceType(GithubServiceType{})
  94. RegisterSocialNetwork("github")
  95. RegisterMakeProofCheckerFunc("github",
  96. func(l RemoteProofChainLink) (ProofChecker, ProofError) {
  97. return NewGithubChecker(l)
  98. })
  99. }
  100. //=============================================================================