authorization.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package oauth2 // import "miniflux.app/v2/internal/oauth2"
  4. import (
  5. "crypto/sha256"
  6. "encoding/base64"
  7. "io"
  8. "golang.org/x/oauth2"
  9. "miniflux.app/v2/internal/crypto"
  10. )
  11. type Authorization struct {
  12. url string
  13. state string
  14. codeVerifier string
  15. }
  16. func (u *Authorization) RedirectURL() string {
  17. return u.url
  18. }
  19. func (u *Authorization) State() string {
  20. return u.state
  21. }
  22. func (u *Authorization) CodeVerifier() string {
  23. return u.codeVerifier
  24. }
  25. func GenerateAuthorization(config *oauth2.Config) *Authorization {
  26. codeVerifier := crypto.GenerateRandomStringHex(32)
  27. sha2 := sha256.New()
  28. io.WriteString(sha2, codeVerifier)
  29. codeChallenge := base64.RawURLEncoding.EncodeToString(sha2.Sum(nil))
  30. state := crypto.GenerateRandomStringHex(24)
  31. authUrl := config.AuthCodeURL(
  32. state,
  33. oauth2.SetAuthURLParam("code_challenge_method", "S256"),
  34. oauth2.SetAuthURLParam("code_challenge", codeChallenge),
  35. )
  36. return &Authorization{
  37. url: authUrl,
  38. state: state,
  39. codeVerifier: codeVerifier,
  40. }
  41. }