init.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package auth
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "github.com/spf13/viper"
  6. "golang.org/x/oauth2"
  7. )
  8. var (
  9. letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  10. stateRand *rand.Rand
  11. googleConfig oauth2.Config
  12. )
  13. func Init(v *viper.Viper) {
  14. googleConfig = oauth2.Config{
  15. // ClientID: "141386065809-og78kt5um5kdtstvlk7m8e00231aadgi.apps.googleusercontent.com",
  16. ClientID: v.GetString("auth.google.client_id"),
  17. // ClientSecret: "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",
  18. ClientSecret: v.GetString("auth.google.client_secret"),
  19. // RedirectURL: "http://localhost:9000/v1/health/callback",
  20. RedirectURL: fmt.Sprintf("%s%s", v.GetString("redirect_url"), v.GetString("auth.google.redirect_uri")),
  21. Scopes: []string{"openid email profile"},
  22. Endpoint: oauth2.Endpoint{
  23. AuthURL: "https://accounts.google.com/o/oauth2/v2/auth",
  24. TokenURL: "https://www.googleapis.com/oauth2/v4/token",
  25. },
  26. }
  27. }
  28. // RandToken Generate a random token
  29. func GenerateOauthState() string {
  30. l := len(letterRunes)
  31. b := make([]rune, 24)
  32. for i := range b {
  33. b[i] = letterRunes[stateRand.Intn(l)]
  34. }
  35. return string(b)
  36. }