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.

126 lines
2.7 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. // +build go1.4
  2. package jwt
  3. import (
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. )
  8. // Implements the RSAPSS family of signing methods signing methods
  9. type SigningMethodRSAPSS struct {
  10. *SigningMethodRSA
  11. Options *rsa.PSSOptions
  12. }
  13. // Specific instances for RS/PS and company
  14. var (
  15. SigningMethodPS256 *SigningMethodRSAPSS
  16. SigningMethodPS384 *SigningMethodRSAPSS
  17. SigningMethodPS512 *SigningMethodRSAPSS
  18. )
  19. func init() {
  20. // PS256
  21. SigningMethodPS256 = &SigningMethodRSAPSS{
  22. &SigningMethodRSA{
  23. Name: "PS256",
  24. Hash: crypto.SHA256,
  25. },
  26. &rsa.PSSOptions{
  27. SaltLength: rsa.PSSSaltLengthAuto,
  28. Hash: crypto.SHA256,
  29. },
  30. }
  31. RegisterSigningMethod(SigningMethodPS256.Alg(), func() SigningMethod {
  32. return SigningMethodPS256
  33. })
  34. // PS384
  35. SigningMethodPS384 = &SigningMethodRSAPSS{
  36. &SigningMethodRSA{
  37. Name: "PS384",
  38. Hash: crypto.SHA384,
  39. },
  40. &rsa.PSSOptions{
  41. SaltLength: rsa.PSSSaltLengthAuto,
  42. Hash: crypto.SHA384,
  43. },
  44. }
  45. RegisterSigningMethod(SigningMethodPS384.Alg(), func() SigningMethod {
  46. return SigningMethodPS384
  47. })
  48. // PS512
  49. SigningMethodPS512 = &SigningMethodRSAPSS{
  50. &SigningMethodRSA{
  51. Name: "PS512",
  52. Hash: crypto.SHA512,
  53. },
  54. &rsa.PSSOptions{
  55. SaltLength: rsa.PSSSaltLengthAuto,
  56. Hash: crypto.SHA512,
  57. },
  58. }
  59. RegisterSigningMethod(SigningMethodPS512.Alg(), func() SigningMethod {
  60. return SigningMethodPS512
  61. })
  62. }
  63. // Implements the Verify method from SigningMethod
  64. // For this verify method, key must be an rsa.PublicKey struct
  65. func (m *SigningMethodRSAPSS) Verify(signingString, signature string, key interface{}) error {
  66. var err error
  67. // Decode the signature
  68. var sig []byte
  69. if sig, err = DecodeSegment(signature); err != nil {
  70. return err
  71. }
  72. var rsaKey *rsa.PublicKey
  73. switch k := key.(type) {
  74. case *rsa.PublicKey:
  75. rsaKey = k
  76. default:
  77. return ErrInvalidKey
  78. }
  79. // Create hasher
  80. if !m.Hash.Available() {
  81. return ErrHashUnavailable
  82. }
  83. hasher := m.Hash.New()
  84. hasher.Write([]byte(signingString))
  85. return rsa.VerifyPSS(rsaKey, m.Hash, hasher.Sum(nil), sig, m.Options)
  86. }
  87. // Implements the Sign method from SigningMethod
  88. // For this signing method, key must be an rsa.PrivateKey struct
  89. func (m *SigningMethodRSAPSS) Sign(signingString string, key interface{}) (string, error) {
  90. var rsaKey *rsa.PrivateKey
  91. switch k := key.(type) {
  92. case *rsa.PrivateKey:
  93. rsaKey = k
  94. default:
  95. return "", ErrInvalidKeyType
  96. }
  97. // Create the hasher
  98. if !m.Hash.Available() {
  99. return "", ErrHashUnavailable
  100. }
  101. hasher := m.Hash.New()
  102. hasher.Write([]byte(signingString))
  103. // Sign the string and return the encoded bytes
  104. if sigBytes, err := rsa.SignPSS(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil), m.Options); err == nil {
  105. return EncodeSegment(sigBytes), nil
  106. } else {
  107. return "", err
  108. }
  109. }