github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/appengine/login/gitkit1/register_handlers.go (about)

     1  package gitkit1
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/gob"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  
    12  	"github.com/google/identity-toolkit-go-client/gitkit"
    13  	gorillaContext "github.com/gorilla/context"
    14  	"github.com/gorilla/sessions"
    15  	"github.com/pbberlin/tools/net/http/htmlfrag"
    16  
    17  	"appengine"
    18  )
    19  
    20  func initCodeBaseDir() {
    21  	var err error
    22  	CodeBaseDirectory, err = os.Getwd()
    23  	if err != nil {
    24  		panic("could not call the code base directory: " + err.Error() + "<br>\n")
    25  	}
    26  	// Make the path working
    27  	CodeBaseDirectory = path.Clean(CodeBaseDirectory) // remove trailing slash
    28  	if !strings.HasSuffix(CodeBaseDirectory, "/") {
    29  		CodeBaseDirectory += "/"
    30  	}
    31  	privateKeyPath = strings.Replace(privateKeyPath, "[CodeBaseDirectory]", CodeBaseDirectory, -1)
    32  
    33  }
    34  
    35  func InitHandlers() {
    36  
    37  	initCodeBaseDir()
    38  
    39  	// Register datatypes such that it can be saved in the session.
    40  	gob.Register(SessionUserKey(0))
    41  	gob.Register(&User{})
    42  
    43  	// Initialize XSRF token key.
    44  	xsrfKey = "My personal very secure XSRF token key"
    45  
    46  	sessKey := []byte("secure-key-234002395432-wsasjasfsfsfsaa-234002395432-wsasjasfsfsfsaa-234002395432-wsasjasfsfsfsaa")
    47  
    48  	// Create a session cookie store.
    49  	cookieStore = sessions.NewCookieStore(
    50  		sessKey[:64],
    51  		sessKey[:32],
    52  	)
    53  
    54  	cookieStore.Options = &sessions.Options{
    55  		MaxAge:   maxSessionIDAge, // Session valid for 30 Minutes.
    56  		HttpOnly: true,
    57  	}
    58  
    59  	// Create identity toolkit client.
    60  	c := &gitkit.Config{
    61  		ServerAPIKey: getConfig(siteName, "serverAPIKey"),
    62  		ClientID:     getConfig(siteName, "clientID"),
    63  		WidgetURL:    WidgetSigninAuthorizedRedirectURL,
    64  	}
    65  	// Service account and private key are not required in GAE Prod.
    66  	// GAE App Identity API is used to identify the app.
    67  	if appengine.IsDevAppServer() {
    68  		c.ServiceAccount = getConfig(siteName, "serviceAccount")
    69  		c.PEMKeyPath = privateKeyPath
    70  	}
    71  	var err error
    72  	gitkitClient, err = gitkit.New(c)
    73  	if err != nil {
    74  		log.Fatal(err)
    75  	}
    76  
    77  	// The gorilla sessions use gorilla request context
    78  	ClearHandler := func(fc http.HandlerFunc) http.Handler {
    79  		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    80  			defer gorillaContext.Clear(r)
    81  			fc(w, r)
    82  		})
    83  	}
    84  
    85  	http.Handle(homeURL, ClearHandler(handleHome))
    86  
    87  	http.Handle(WidgetSigninAuthorizedRedirectURL, ClearHandler(handleWidget))
    88  	http.Handle(signOutURL, ClearHandler(handleSignOut))
    89  
    90  	http.Handle(signinLandingDefaultURL, ClearHandler(handleSigninSuccessLanding))
    91  	http.Handle(signoutLandingDefaultURL, ClearHandler(handleSignOutLanding))
    92  
    93  	http.HandleFunc(accountChooserBrandingURL, accountChooserBranding)
    94  }
    95  
    96  // userinterface rendered to HTML - not only the strings for title and url
    97  func BackendUIRendered() *bytes.Buffer {
    98  	var b1 = new(bytes.Buffer)
    99  
   100  	htmlfrag.Wb(b1, "Login GitKit", homeURL, "opposite of appengine login")
   101  	htmlfrag.Wb(b1, "Signin", WidgetSigninAuthorizedRedirectURL+"?mode=select", "")
   102  	htmlfrag.Wb(b1, "Success Landing", signinLandingDefaultURL, "")
   103  	htmlfrag.Wb(b1, "Signout", signOutURL, "")
   104  	return b1
   105  }