authorization.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "golang.org/x/oauth2"
  8. "miniflux.app/v2/internal/crypto"
  9. )
  10. type Authorization struct {
  11. url string
  12. state string
  13. codeVerifier string
  14. }
  15. func (u *Authorization) RedirectURL() string {
  16. return u.url
  17. }
  18. func (u *Authorization) State() string {
  19. return u.state
  20. }
  21. func (u *Authorization) CodeVerifier() string {
  22. return u.codeVerifier
  23. }
  24. func GenerateAuthorization(config *oauth2.Config) *Authorization {
  25. codeVerifier := crypto.GenerateRandomStringHex(32)
  26. sum := sha256.Sum256([]byte(codeVerifier))
  27. state := crypto.GenerateRandomStringHex(24)
  28. authUrl := config.AuthCodeURL(
  29. state,
  30. oauth2.SetAuthURLParam("code_challenge_method", "S256"),
  31. oauth2.SetAuthURLParam("code_challenge", base64.RawURLEncoding.EncodeToString(sum[:])),
  32. )
  33. return &Authorization{
  34. url: authUrl,
  35. state: state,
  36. codeVerifier: codeVerifier,
  37. }
  38. }