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.

147 lines
3.5 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"
  4. "crypto/ecdsa"
  5. "crypto/rand"
  6. "errors"
  7. "math/big"
  8. )
  9. var (
  10. // Sadly this is missing from crypto/ecdsa compared to crypto/rsa
  11. ErrECDSAVerification = errors.New("crypto/ecdsa: verification error")
  12. )
  13. // Implements the ECDSA family of signing methods signing methods
  14. type SigningMethodECDSA struct {
  15. Name string
  16. Hash crypto.Hash
  17. KeySize int
  18. CurveBits int
  19. }
  20. // Specific instances for EC256 and company
  21. var (
  22. SigningMethodES256 *SigningMethodECDSA
  23. SigningMethodES384 *SigningMethodECDSA
  24. SigningMethodES512 *SigningMethodECDSA
  25. )
  26. func init() {
  27. // ES256
  28. SigningMethodES256 = &SigningMethodECDSA{"ES256", crypto.SHA256, 32, 256}
  29. RegisterSigningMethod(SigningMethodES256.Alg(), func() SigningMethod {
  30. return SigningMethodES256
  31. })
  32. // ES384
  33. SigningMethodES384 = &SigningMethodECDSA{"ES384", crypto.SHA384, 48, 384}
  34. RegisterSigningMethod(SigningMethodES384.Alg(), func() SigningMethod {
  35. return SigningMethodES384
  36. })
  37. // ES512
  38. SigningMethodES512 = &SigningMethodECDSA{"ES512", crypto.SHA512, 66, 521}
  39. RegisterSigningMethod(SigningMethodES512.Alg(), func() SigningMethod {
  40. return SigningMethodES512
  41. })
  42. }
  43. func (m *SigningMethodECDSA) Alg() string {
  44. return m.Name
  45. }
  46. // Implements the Verify method from SigningMethod
  47. // For this verify method, key must be an ecdsa.PublicKey struct
  48. func (m *SigningMethodECDSA) Verify(signingString, signature string, key interface{}) error {
  49. var err error
  50. // Decode the signature
  51. var sig []byte
  52. if sig, err = DecodeSegment(signature); err != nil {
  53. return err
  54. }
  55. // Get the key
  56. var ecdsaKey *ecdsa.PublicKey
  57. switch k := key.(type) {
  58. case *ecdsa.PublicKey:
  59. ecdsaKey = k
  60. default:
  61. return ErrInvalidKeyType
  62. }
  63. if len(sig) != 2*m.KeySize {
  64. return ErrECDSAVerification
  65. }
  66. r := big.NewInt(0).SetBytes(sig[:m.KeySize])
  67. s := big.NewInt(0).SetBytes(sig[m.KeySize:])
  68. // Create hasher
  69. if !m.Hash.Available() {
  70. return ErrHashUnavailable
  71. }
  72. hasher := m.Hash.New()
  73. hasher.Write([]byte(signingString))
  74. // Verify the signature
  75. if verifystatus := ecdsa.Verify(ecdsaKey, hasher.Sum(nil), r, s); verifystatus == true {
  76. return nil
  77. } else {
  78. return ErrECDSAVerification
  79. }
  80. }
  81. // Implements the Sign method from SigningMethod
  82. // For this signing method, key must be an ecdsa.PrivateKey struct
  83. func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) (string, error) {
  84. // Get the key
  85. var ecdsaKey *ecdsa.PrivateKey
  86. switch k := key.(type) {
  87. case *ecdsa.PrivateKey:
  88. ecdsaKey = k
  89. default:
  90. return "", ErrInvalidKeyType
  91. }
  92. // Create the hasher
  93. if !m.Hash.Available() {
  94. return "", ErrHashUnavailable
  95. }
  96. hasher := m.Hash.New()
  97. hasher.Write([]byte(signingString))
  98. // Sign the string and return r, s
  99. if r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, hasher.Sum(nil)); err == nil {
  100. curveBits := ecdsaKey.Curve.Params().BitSize
  101. if m.CurveBits != curveBits {
  102. return "", ErrInvalidKey
  103. }
  104. keyBytes := curveBits / 8
  105. if curveBits%8 > 0 {
  106. keyBytes += 1
  107. }
  108. // We serialize the outpus (r and s) into big-endian byte arrays and pad
  109. // them with zeros on the left to make sure the sizes work out. Both arrays
  110. // must be keyBytes long, and the output must be 2*keyBytes long.
  111. rBytes := r.Bytes()
  112. rBytesPadded := make([]byte, keyBytes)
  113. copy(rBytesPadded[keyBytes-len(rBytes):], rBytes)
  114. sBytes := s.Bytes()
  115. sBytesPadded := make([]byte, keyBytes)
  116. copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
  117. out := append(rBytesPadded, sBytesPadded...)
  118. return EncodeSegment(out), nil
  119. } else {
  120. return "", err
  121. }
  122. }