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.

108 lines
3.3 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. "encoding/base64"
  4. "encoding/json"
  5. "strings"
  6. "time"
  7. )
  8. // TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time).
  9. // You can override it to use another time value. This is useful for testing or if your
  10. // server uses a different time zone than your tokens.
  11. var TimeFunc = time.Now
  12. // Parse methods use this callback function to supply
  13. // the key for verification. The function receives the parsed,
  14. // but unverified Token. This allows you to use properties in the
  15. // Header of the token (such as `kid`) to identify which key to use.
  16. type Keyfunc func(*Token) (interface{}, error)
  17. // A JWT Token. Different fields will be used depending on whether you're
  18. // creating or parsing/verifying a token.
  19. type Token struct {
  20. Raw string // The raw token. Populated when you Parse a token
  21. Method SigningMethod // The signing method used or to be used
  22. Header map[string]interface{} // The first segment of the token
  23. Claims Claims // The second segment of the token
  24. Signature string // The third segment of the token. Populated when you Parse a token
  25. Valid bool // Is the token valid? Populated when you Parse/Verify a token
  26. }
  27. // Create a new Token. Takes a signing method
  28. func New(method SigningMethod) *Token {
  29. return NewWithClaims(method, MapClaims{})
  30. }
  31. func NewWithClaims(method SigningMethod, claims Claims) *Token {
  32. return &Token{
  33. Header: map[string]interface{}{
  34. "typ": "JWT",
  35. "alg": method.Alg(),
  36. },
  37. Claims: claims,
  38. Method: method,
  39. }
  40. }
  41. // Get the complete, signed token
  42. func (t *Token) SignedString(key interface{}) (string, error) {
  43. var sig, sstr string
  44. var err error
  45. if sstr, err = t.SigningString(); err != nil {
  46. return "", err
  47. }
  48. if sig, err = t.Method.Sign(sstr, key); err != nil {
  49. return "", err
  50. }
  51. return strings.Join([]string{sstr, sig}, "."), nil
  52. }
  53. // Generate the signing string. This is the
  54. // most expensive part of the whole deal. Unless you
  55. // need this for something special, just go straight for
  56. // the SignedString.
  57. func (t *Token) SigningString() (string, error) {
  58. var err error
  59. parts := make([]string, 2)
  60. for i, _ := range parts {
  61. var jsonValue []byte
  62. if i == 0 {
  63. if jsonValue, err = json.Marshal(t.Header); err != nil {
  64. return "", err
  65. }
  66. } else {
  67. if jsonValue, err = json.Marshal(t.Claims); err != nil {
  68. return "", err
  69. }
  70. }
  71. parts[i] = EncodeSegment(jsonValue)
  72. }
  73. return strings.Join(parts, "."), nil
  74. }
  75. // Parse, validate, and return a token.
  76. // keyFunc will receive the parsed token and should return the key for validating.
  77. // If everything is kosher, err will be nil
  78. func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
  79. return new(Parser).Parse(tokenString, keyFunc)
  80. }
  81. func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
  82. return new(Parser).ParseWithClaims(tokenString, claims, keyFunc)
  83. }
  84. // Encode JWT specific base64url encoding with padding stripped
  85. func EncodeSegment(seg []byte) string {
  86. return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
  87. }
  88. // Decode JWT specific base64url encoding with padding stripped
  89. func DecodeSegment(seg string) ([]byte, error) {
  90. if l := len(seg) % 4; l > 0 {
  91. seg += strings.Repeat("=", 4-l)
  92. }
  93. return base64.URLEncoding.DecodeString(seg)
  94. }