config.go 713 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package oauth
  2. import (
  3. "crypto/rand"
  4. "encoding/base64"
  5. "golang.org/x/oauth2"
  6. )
  7. type Config struct {
  8. ClientID string
  9. ClientSecret string
  10. Scopes []string
  11. BaseURL string
  12. }
  13. func NewGithubClient(cfg *Config) *oauth2.Config {
  14. return &oauth2.Config{
  15. ClientID: cfg.ClientID,
  16. ClientSecret: cfg.ClientSecret,
  17. Endpoint: oauth2.Endpoint{
  18. AuthURL: "https://github.com/login/oauth/authorize",
  19. TokenURL: "https://github.com/login/oauth/access_token",
  20. },
  21. RedirectURL: cfg.BaseURL + "/api/oauth/github/callback",
  22. Scopes: cfg.Scopes,
  23. }
  24. }
  25. func CreateRandomState() string {
  26. b := make([]byte, 16)
  27. rand.Read(b)
  28. state := base64.URLEncoding.EncodeToString(b)
  29. return state
  30. }