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

     1  package gitkit
     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: serverAPIKey,
    62  		ClientID:     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 = 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(successLandingURL, ClearHandler(HandleSuccess))
    89  
    90  	http.Handle(signOutURL, ClearHandler(handleSignOut))
    91  	http.Handle(signoutLandingURL, ClearHandler(handleSignoutLanding))
    92  
    93  	http.Handle(updateURL, ClearHandler(handleUpdate))
    94  
    95  	http.HandleFunc(accountChooserBrandingURL, accountChooserBranding)
    96  }
    97  
    98  // userinterface rendered to HTML - not only the strings for title and url
    99  func BackendUIRendered() *bytes.Buffer {
   100  	var b1 = new(bytes.Buffer)
   101  
   102  	htmlfrag.Wb(b1, "Login GitKit", homeURL, "opposite of appengine login")
   103  	htmlfrag.Wb(b1, "Signin", widgetSigninAuthorizedRedirectURL+"?mode=select", "")
   104  	htmlfrag.Wb(b1, "Success Landing", successLandingURL, "")
   105  	htmlfrag.Wb(b1, "Signout", signOutURL, "")
   106  	htmlfrag.Wb(b1, "Signout Landing", signoutLandingURL, "")
   107  	return b1
   108  }