githuboauth

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(config *oauth2.Config) client

Types

type Agent

type Agent struct {
	// contains filtered or unexported fields
}

Agent represents an agent that takes care GitHub authentication process such as handles login request from users or handles redirection from GitHub OAuth server.

func NewAgent

func NewAgent(config *Config, logger *logrus.Entry) *Agent

NewAgent returns a new GitHub OAuth Agent.

func (*Agent) GetLogin

func (ga *Agent) GetLogin(r *http.Request, identifier AuthenticatedUserIdentifier) (string, error)

GetLogin returns the username of the already authenticated GitHub user.

func (*Agent) HandleLogin

func (ga *Agent) HandleLogin(client OAuthClient, secure bool) http.HandlerFunc

HandleLogin handles GitHub login request from front-end. It starts a new git oauth session and redirect user to GitHub OAuth end-point for authentication.

func (*Agent) HandleLogout

func (ga *Agent) HandleLogout(client OAuthClient) http.HandlerFunc

HandleLogout handles GitHub logout request from front-end. It invalidates cookie sessions and redirect back to the front page.

func (*Agent) HandleRedirect

func (ga *Agent) HandleRedirect(client OAuthClient, identifier AuthenticatedUserIdentifier, secure bool) http.HandlerFunc

HandleRedirect handles the redirection from GitHub. It exchanges the code from redirect URL for user access token. The access token is then saved to the cookie and the page is redirected to the final destination in the config, which should be the front-end.

type AuthenticatedUserIdentifier

type AuthenticatedUserIdentifier interface {
	LoginForRequester(requester, token string) (string, error)
}

AuthenticatedUserIdentifier knows how to get the identity of an authenticated user

func NewAuthenticatedUserIdentifier

func NewAuthenticatedUserIdentifier(options *flagutil.GitHubOptions) AuthenticatedUserIdentifier

type Config

type Config struct {
	ClientID     string   `json:"client_id"`
	ClientSecret string   `json:"client_secret"`
	RedirectURL  string   `json:"redirect_url"`
	Scopes       []string `json:"scopes,omitempty"`

	CookieStore *sessions.CookieStore `json:"-"`
}

Config is a config for requesting users access tokens from GitHub API. It also has a Cookie Store that retains user credentials deriving from GitHub API.

func (*Config) InitGitHubOAuthConfig

func (c *Config) InitGitHubOAuthConfig(cookie *sessions.CookieStore)

InitGitHubOAuthConfig creates an OAuthClient using GitHubOAuth config and a Cookie Store to retain user credentials.

type OAuthClient

type OAuthClient interface {
	WithFinalRedirectURL(url string) (OAuthClient, error)
	// Exchanges code from GitHub OAuth redirect for user access token.
	Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
	// Returns a URL to GitHub's OAuth 2.0 consent page. The state is a token to protect the user
	// from an XSRF attack.
	AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
}

OAuthClient is an interface for a GitHub OAuth client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL