githuboauth.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "encoding/gob"
  16. "github.com/gorilla/sessions"
  17. "golang.org/x/oauth2"
  18. )
  19. // Cookie holds the secret returned from github that authenticates the user who authorized this app.
  20. type Cookie struct {
  21. Secret string `json:"secret,omitempty"`
  22. }
  23. // GithubOAuthConfig is a config for requesting users access tokens from Github API. It also has
  24. // a Cookie Store that retains user credentials deriving from Github API.
  25. type GithubOAuthConfig struct {
  26. ClientID string `json:"client_id"`
  27. ClientSecret string `json:"client_secret"`
  28. RedirectURL string `json:"redirect_url"`
  29. Scopes []string `json:"scopes,omitempty"`
  30. FinalRedirectURL string `json:"final_redirect_url"`
  31. CookieStore *sessions.CookieStore `json:"-"`
  32. }
  33. // InitGithubOAuthConfig creates an OAuthClient using GithubOAuth config and a Cookie Store
  34. // to retain user credentials.
  35. func (gac *GithubOAuthConfig) InitGithubOAuthConfig(cookie *sessions.CookieStore) {
  36. gob.Register(&oauth2.Token{})
  37. gac.CookieStore = cookie
  38. }