authsession

package module
v0.0.0-...-0ffde1f Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: MIT Imports: 10 Imported by: 0

README

authsession

Go / Golang , Google Oauth2 authentication and sessions handling to use with webpages.

Oauth2

Oauth2 for authenticating towards Google. Will return the personalia of the user logged in.

You need to register an Oauth2 App at google cloud, with a callback similar to the one specified in the code example below.

After registering an app at Google Cloud, you need to export environment variables like the example below

export cookiestorekey=some-cookie-store-key-here
export googlekey=some-google-key-here
export googlesecret=some-google-secret-here

Sessions

Using Gorilla Sessions for session handling, and storing all session values in a session token. This token can be checked for the key authenticated and if true user will get access to the page requested.

A wrapper function is also included, and you wrap this around the HandlerFunc you define in your http.HandleFunc statement. Example below.

    a := authsession.NewAuth("http://", "localhost", ":8080")
    //Run will start the http.HandleFunc's needed for authentication.
    //The HandleFunc's started with Run() are :
    //http.HandleFunc("/login", a.login)
	//http.HandleFunc("/logout", a.logout)
    //http.HandleFunc("/callback", a.handleGoogleCallback)
    //
    //Then you can specify and use /login, /logout in your html page, and you use the /callback when you registert the callback url in the Oauth2 app at google cloud. Example of callback string for google is `http://localhost:8080/callback`
	a.Run()

    http.HandleFunc("/upload", a.IsAuthenticated(d.uploadImage))

    err := http.ListenAndServe(*hostListen+*port, nil)
	if err != nil {
		log.Println("error: ListenAndServer failed: ", err)
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

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

Auth is used for the authentication handlers, and hold all the values needed for authentication.

func NewAuth

func NewAuth(proto string, host string, port string, cookieStoreKey string, clientIDKey string, clientSecret string) (*Auth, *sessions.CookieStore)

NewAuth will return *auth and a *sessions.CookieStore, with a prepared OauthConfig and CookieStore set. proto, is either http or https, host, is the name of your sever, like example.com or localhost or..., port, for example :8080, cookieStoreKey, is the secret key used for the cookie storage, clientIDKey, is the Client ID key found in the google developer console for your oauth app, clientSecret, is the client secret found in the google developer console for your oauth app.

func (*Auth) IsAuthenticated

func (a *Auth) IsAuthenticated(h http.HandlerFunc) http.HandlerFunc

IsAuthenticated is a wrapper to put around handlers you want to protect with an authenticated user.

func (*Auth) Run

func (a *Auth) Run()

Run will start the auth, which basically is to run the HandleFunc's needed.

Jump to

Keyboard shortcuts

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