func main() { log.SetLevel(log.DebugLevel) log.Info("Gathering configuration") resourceProvider := configuration.NewResourceProvider("en-us") settingsProvider := configuration.NewSettingsProvider() setupDatabase(settingsProvider) log.Info("Reading private key") privateKey, err := ioutil.ReadFile(PRIVATE_KEY_PATH) if err != nil { log.Fatal(err) return } log.Info("Reading public key") publicKey, err := ioutil.ReadFile(PUBLIC_KEY_PATH) if err != nil { log.Fatal(err) return } var roleACL []middleware.RoleAuthorisation roleACL = append(roleACL, middleware.RoleAuthorisation{ AllowAnonymous: true, UrlMatcher: utils.NewRegexMatcher([]string{`\/rpc\/(v\d+)\/Account.Login`}), }) roleACL = append(roleACL, middleware.RoleAuthorisation{ RequiredRole: "user", UrlMatcher: utils.NewRegexMatcher([]string{`\/rpc\/(v\d+)\/Hackathon.(.*)`}), }) excludeFromJWTRegex := []string{`\/rpc\/(v\d+)\/Account.Login`} claimProvider := security.NewContextClaimProvider() dbProvider := data.NewContextDbProvider() unitOfWorkFactory := data.NewSqlUnitOfWorkFactory(dbProvider) rpcHandler := createRPCServer(&privateKey, resourceProvider, claimProvider, unitOfWorkFactory) rpcHandler = addOpenDbFilter(rpcHandler, dbProvider, settingsProvider) rpcHandler = addRoleFilter(rpcHandler, &roleACL, claimProvider) rpcHandler = addJWTFilter(rpcHandler, &publicKey, excludeFromJWTRegex, claimProvider) rpcHandler = addContextClearFilter(rpcHandler) //rpcHandler = addPanicFilter(rpcHandler) debug := true if debug { fileServerApp := http.FileServer(http.Dir("./web/app")) fileServerComp := http.FileServer(http.Dir("./web/bower_components")) http.Handle("/bower_components/", http.StripPrefix("/bower_components/", fileServerComp)) http.Handle("/", fileServerApp) } else { fileServer := http.FileServer(http.Dir("./web/dist")) fileServer = middleware.NewRequestPerformanceLogFilter(fileServer) } http.Handle("/rpc/v1/", rpcHandler) log.Info("Starting bulbtransit") //Start server http.ListenAndServe(":8000", nil) }
func addJWTFilter(handlerToProtect http.Handler, publicKey *[]byte, excludedRegex []string, claimProvider security.ClaimProvider) http.Handler { log.Info("Protecting server with JWT filter") result := middleware.NewJWTFilter(handlerToProtect, publicKey, utils.NewRegexMatcher(excludedRegex), claimProvider) return result }