You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6 KiB

Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
  1. package jwt
  2. import (
  3. "crypto/subtle"
  4. "fmt"
  5. "time"
  6. )
  7. // For a type to be a Claims object, it must just have a Valid method that determines
  8. // if the token is invalid for any supported reason
  9. type Claims interface {
  10. Valid() error
  11. }
  12. // Structured version of Claims Section, as referenced at
  13. // https://tools.ietf.org/html/rfc7519#section-4.1
  14. // See examples for how to use this with your own claim types
  15. type StandardClaims struct {
  16. Audience string `json:"aud,omitempty"`
  17. ExpiresAt int64 `json:"exp,omitempty"`
  18. Id string `json:"jti,omitempty"`
  19. IssuedAt int64 `json:"iat,omitempty"`
  20. Issuer string `json:"iss,omitempty"`
  21. NotBefore int64 `json:"nbf,omitempty"`
  22. Subject string `json:"sub,omitempty"`
  23. }
  24. // Validates time based claims "exp, iat, nbf".
  25. // There is no accounting for clock skew.
  26. // As well, if any of the above claims are not in the token, it will still
  27. // be considered a valid claim.
  28. func (c StandardClaims) Valid() error {
  29. vErr := new(ValidationError)
  30. now := TimeFunc().Unix()
  31. // The claims below are optional, by default, so if they are set to the
  32. // default value in Go, let's not fail the verification for them.
  33. if c.VerifyExpiresAt(now, false) == false {
  34. delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
  35. vErr.Inner = fmt.Errorf("token is expired by %v", delta)
  36. vErr.Errors |= ValidationErrorExpired
  37. }
  38. if c.VerifyIssuedAt(now, false) == false {
  39. vErr.Inner = fmt.Errorf("Token used before issued")
  40. vErr.Errors |= ValidationErrorIssuedAt
  41. }
  42. if c.VerifyNotBefore(now, false) == false {
  43. vErr.Inner = fmt.Errorf("token is not valid yet")
  44. vErr.Errors |= ValidationErrorNotValidYet
  45. }
  46. if vErr.valid() {
  47. return nil
  48. }
  49. return vErr
  50. }
  51. // Compares the aud claim against cmp.
  52. // If required is false, this method will return true if the value matches or is unset
  53. func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
  54. return verifyAud(c.Audience, cmp, req)
  55. }
  56. // Compares the exp claim against cmp.
  57. // If required is false, this method will return true if the value matches or is unset
  58. func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
  59. return verifyExp(c.ExpiresAt, cmp, req)
  60. }
  61. // Compares the iat claim against cmp.
  62. // If required is false, this method will return true if the value matches or is unset
  63. func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
  64. return verifyIat(c.IssuedAt, cmp, req)
  65. }
  66. // Compares the iss claim against cmp.
  67. // If required is false, this method will return true if the value matches or is unset
  68. func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool {
  69. return verifyIss(c.Issuer, cmp, req)
  70. }
  71. // Compares the nbf claim against cmp.
  72. // If required is false, this method will return true if the value matches or is unset
  73. func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
  74. return verifyNbf(c.NotBefore, cmp, req)
  75. }
  76. // ----- helpers
  77. func verifyAud(aud string, cmp string, required bool) bool {
  78. if aud == "" {
  79. return !required
  80. }
  81. if subtle.ConstantTimeCompare([]byte(aud), []byte(cmp)) != 0 {
  82. return true
  83. } else {
  84. return false
  85. }
  86. }
  87. func verifyExp(exp int64, now int64, required bool) bool {
  88. if exp == 0 {
  89. return !required
  90. }
  91. return now <= exp
  92. }
  93. func verifyIat(iat int64, now int64, required bool) bool {
  94. if iat == 0 {
  95. return !required
  96. }
  97. return now >= iat
  98. }
  99. func verifyIss(iss string, cmp string, required bool) bool {
  100. if iss == "" {
  101. return !required
  102. }
  103. if subtle.ConstantTimeCompare([]byte(iss), []byte(cmp)) != 0 {
  104. return true
  105. } else {
  106. return false
  107. }
  108. }
  109. func verifyNbf(nbf int64, now int64, required bool) bool {
  110. if nbf == 0 {
  111. return !required
  112. }
  113. return now >= nbf
  114. }