helpers.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package indieauth
  2. import (
  3. "crypto/sha256"
  4. "encoding/base64"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "time"
  10. "github.com/andybalholm/cascadia"
  11. "github.com/pkg/errors"
  12. "golang.org/x/net/html"
  13. )
  14. func createAuthRequest(authDestination, userID, displayName, accessToken, baseServer string) (*Request, error) {
  15. authURL, err := url.Parse(authDestination)
  16. if err != nil {
  17. return nil, errors.Wrap(err, "unable to parse IndieAuth destination")
  18. }
  19. authEndpointURL, err := getAuthEndpointFromURL(authURL.String())
  20. if err != nil {
  21. return nil, errors.Wrap(err, "unable to get IndieAuth endpoint from destination URL")
  22. }
  23. baseServerURL, err := url.Parse(baseServer)
  24. if err != nil {
  25. return nil, errors.Wrap(err, "unable to parse local owncast base server URL")
  26. }
  27. callbackURL := *baseServerURL
  28. callbackURL.Path = "/api/auth/indieauth/callback"
  29. codeVerifier := randString(50)
  30. codeChallenge := createCodeChallenge(codeVerifier)
  31. state := randString(20)
  32. responseType := "code"
  33. clientID := baseServerURL.String() // Our local URL
  34. codeChallengeMethod := "S256"
  35. redirect := *authEndpointURL
  36. q := authURL.Query()
  37. q.Add("response_type", responseType)
  38. q.Add("client_id", clientID)
  39. q.Add("state", state)
  40. q.Add("code_challenge_method", codeChallengeMethod)
  41. q.Add("code_challenge", codeChallenge)
  42. q.Add("me", authURL.String())
  43. q.Add("redirect_uri", callbackURL.String())
  44. redirect.RawQuery = q.Encode()
  45. return &Request{
  46. Me: authURL,
  47. UserID: userID,
  48. DisplayName: displayName,
  49. CurrentAccessToken: accessToken,
  50. Endpoint: authEndpointURL,
  51. ClientID: baseServer,
  52. CodeVerifier: codeVerifier,
  53. CodeChallenge: codeChallenge,
  54. State: state,
  55. Redirect: &redirect,
  56. Callback: &callbackURL,
  57. Timestamp: time.Now(),
  58. }, nil
  59. }
  60. func getAuthEndpointFromURL(urlstring string) (*url.URL, error) {
  61. htmlDocScrapeURL, err := url.Parse(urlstring)
  62. if err != nil {
  63. return nil, errors.Wrap(err, "unable to parse URL")
  64. }
  65. if htmlDocScrapeURL.Scheme != "https" {
  66. return nil, fmt.Errorf("url must be https")
  67. }
  68. r, err := http.Get(htmlDocScrapeURL.String()) // nolint:gosec
  69. if err != nil {
  70. return nil, err
  71. }
  72. defer r.Body.Close()
  73. scrapedHTMLDocument, err := html.Parse(r.Body)
  74. if err != nil {
  75. return nil, errors.Wrap(err, "unable to parse html at remote auth host")
  76. }
  77. authorizationEndpointTag := cascadia.MustCompile("link[rel=authorization_endpoint]").MatchAll(scrapedHTMLDocument)
  78. if len(authorizationEndpointTag) == 0 {
  79. return nil, fmt.Errorf("url does not support indieauth")
  80. }
  81. for _, attr := range authorizationEndpointTag[len(authorizationEndpointTag)-1].Attr {
  82. if attr.Key == "href" {
  83. u, err := url.Parse(attr.Val)
  84. if err != nil {
  85. return nil, errors.Wrap(err, "unable to parse authorization endpoint")
  86. }
  87. // If it is a relative URL we an fill in the missing components
  88. // by using the original URL we scraped, since it is the same host.
  89. if u.Scheme == "" {
  90. u.Scheme = htmlDocScrapeURL.Scheme
  91. }
  92. if u.Host == "" {
  93. u.Host = htmlDocScrapeURL.Host
  94. }
  95. return u, nil
  96. }
  97. }
  98. return nil, fmt.Errorf("unable to find href value for authorization_endpoint")
  99. }
  100. func createCodeChallenge(codeVerifier string) string {
  101. sha256hash := sha256.Sum256([]byte(codeVerifier))
  102. encodedHashedCode := strings.TrimRight(base64.URLEncoding.EncodeToString(sha256hash[:]), "=")
  103. return encodedHashedCode
  104. }