doi.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package route
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "github.com/G-Node/gogs/internal/context"
  7. "github.com/G-Node/gogs/internal/setting"
  8. "github.com/G-Node/libgin/libgin"
  9. log "gopkg.in/clog.v1"
  10. )
  11. // RequestDOI sends a registration request to the configured DOI service
  12. func RequestDOI(c *context.Context) {
  13. if !c.Repo.IsAdmin() {
  14. c.Status(http.StatusUnauthorized)
  15. return
  16. }
  17. username := c.User.Name
  18. realname := c.User.FullName
  19. repo := c.Repo.Repository.FullName()
  20. email := c.User.Email
  21. data := libgin.DOIRequestData{
  22. Username: username,
  23. Realname: realname,
  24. Repository: repo,
  25. Email: email,
  26. }
  27. log.Trace("Encrypting data for DOI: %+v", data)
  28. dataj, _ := json.Marshal(data)
  29. regrequest, err := libgin.EncryptURLString([]byte(setting.DOI.Key), string(dataj))
  30. if err != nil {
  31. log.Error(2, "Could not encrypt secret key: %s", err)
  32. c.Status(http.StatusInternalServerError)
  33. return
  34. }
  35. doiurl, err := url.Parse(setting.DOI.URL + "/register") // TODO: Handle error by notifying admin email
  36. if err != nil {
  37. log.Error(2, "Failed to parse DOI URL: %s", setting.DOI.URL)
  38. }
  39. params := url.Values{}
  40. params.Add("regrequest", regrequest)
  41. doiurl.RawQuery = params.Encode()
  42. target, _ := url.PathUnescape(doiurl.String())
  43. log.Trace(target)
  44. c.RawRedirect(target)
  45. }