github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/appengine/login/gitkit/unused_example_code.go (about) 1 package gitkit 2 3 import ( 4 "fmt" 5 "html/template" 6 "net/http" 7 "time" 8 9 "appengine/mail" 10 11 "github.com/adg/xsrftoken" 12 "github.com/pbberlin/tools/net/http/htmlfrag" // issues certificates (tokens) for possible http requests, making other requests impossible 13 14 "github.com/google/identity-toolkit-go-client/gitkit" 15 16 gorillaContext "github.com/gorilla/context" 17 18 "google.golang.org/appengine" 19 aelog "google.golang.org/appengine/log" 20 21 aeOrig "appengine" 22 ) 23 24 const home3 = `{{if .User}} 25 <p>Tired of FavWeekday?</p> 26 <form method="POST" action="{{.DeleteAccountURL}}"> 27 <input type="hidden" name="xsrftoken" value="{{.DeleteAccountXSRFToken}}"> 28 <button type="submit">delete account</button> 29 </form> 30 {{end}}` 31 32 const ( 33 deleteAccountURL = "/auth/deleteAccount" 34 oobActionURL = "/auth/send-email" 35 ) 36 37 func UNUSEDinit() { 38 39 // The gorilla sessions use gorilla request context 40 ClearHandler := func(fc http.HandlerFunc) http.Handler { 41 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 42 defer gorillaContext.Clear(r) 43 fc(w, r) 44 }) 45 } 46 47 http.Handle(deleteAccountURL, ClearHandler(handleDeleteAccount)) 48 http.Handle(oobActionURL, ClearHandler(handleOOBAction)) 49 } 50 51 func UNUSEDhandleHome(w http.ResponseWriter, r *http.Request) { 52 53 u := CurrentUser(r) 54 var d time.Weekday 55 if u != nil { 56 d = weekdayForUser(r, u) 57 } 58 saveCurrentUser(r, w, u) 59 var xf, xd string 60 if u != nil { 61 xf = xsrftoken.Generate(xsrfKey, u.ID, updateURL) 62 xd = xsrftoken.Generate(xsrfKey, u.ID, deleteAccountURL) 63 } 64 65 homeTemplate := getHomeTpl(w, r) 66 homeTemplate.Execute(w, map[string]interface{}{ 67 "CookieDump": template.HTML(htmlfrag.CookieDump(r)), 68 "WidgetURL": widgetSigninAuthorizedRedirectURL, 69 "SignOutURL": signOutURL, 70 "User": u, 71 "WeekdayIndex": d, 72 "Weekdays": weekdays, 73 "UpdateWeekdayURL": updateURL, 74 "UpdateWeekdayXSRFToken": xf, 75 "DeleteAccountURL": deleteAccountURL, 76 "DeleteAccountXSRFToken": xd, 77 }) 78 } 79 80 func handleOOBAction(w http.ResponseWriter, r *http.Request) { 81 c := appengine.NewContext(r) 82 // Create an identity toolkit client associated with the GAE context. 83 client, err := gitkit.NewWithContext(c, gitkitClient) 84 if err != nil { 85 aelog.Errorf(c, "Failed to create a gitkit.Client with a context: %s", err) 86 w.Write([]byte(gitkit.ErrorResponse(err))) 87 return 88 } 89 resp, err := client.GenerateOOBCode(r) 90 if err != nil { 91 aelog.Errorf(c, "Failed to get an OOB code: %s", err) 92 w.Write([]byte(gitkit.ErrorResponse(err))) 93 return 94 } 95 msg := &mail.Message{ 96 Sender: "FavWeekday Support <support@favweekday.appspot.com>", 97 } 98 switch resp.Action { 99 case gitkit.OOBActionResetPassword: 100 msg.Subject = "Reset your FavWeekday account password" 101 msg.HTMLBody = fmt.Sprintf(emailTemplateResetPassword, resp.Email, resp.OOBCodeURL.String()) 102 msg.To = []string{resp.Email} 103 case gitkit.OOBActionChangeEmail: 104 msg.Subject = "FavWeekday account email address change confirmation" 105 msg.HTMLBody = fmt.Sprintf(emailTemplateChangeEmail, resp.Email, resp.NewEmail, resp.OOBCodeURL.String()) 106 msg.To = []string{resp.NewEmail} 107 case gitkit.OOBActionVerifyEmail: 108 msg.Subject = "FavWeekday account registration confirmation" 109 msg.HTMLBody = fmt.Sprintf(emailTemplateVerifyEmail, resp.OOBCodeURL.String()) 110 msg.To = []string{resp.Email} 111 } 112 c2 := aeOrig.NewContext(r) 113 if err := mail.Send(c2, msg); err != nil { 114 aelog.Errorf(c, "Failed to send %s message to user %s: %s", resp.Action, resp.Email, err) 115 w.Write([]byte(gitkit.ErrorResponse(err))) 116 return 117 } 118 w.Write([]byte(gitkit.SuccessResponse())) 119 } 120 121 /* 122 123 Failed to delete user {ID:14423325142879445183 Email:peter.buchmann.68@gmail.com 124 Name:Peter Buchmann EmailVerified:true}: 125 googleapi: Error 400: INVALID_LOCAL_ID, invalid 126 127 Failed to delete 00880189686365773816 128 129 130 Failed to delete user {ID: }: googleapi: Error 400: INVALID_LOCAL_ID, invalid 131 */ 132 func handleDeleteAccount(w http.ResponseWriter, r *http.Request) { 133 c := appengine.NewContext(r) 134 var ( 135 client *gitkit.Client 136 err error 137 ) 138 // Check if there is a signed in user. 139 u := CurrentUser(r) 140 if u == nil { 141 aelog.Errorf(c, "No signed in user for updating") 142 goto out 143 } 144 // Validate XSRF token first. 145 if !xsrftoken.Valid(r.PostFormValue(xsrfTokenName), xsrfKey, u.ID, deleteAccountURL) { 146 aelog.Errorf(c, "XSRF token validation failed") 147 goto out 148 } 149 // Create an identity toolkit client associated with the GAE context. 150 client, err = gitkit.NewWithContext(c, gitkitClient) 151 if err != nil { 152 aelog.Errorf(c, "Failed to create a gitkit.Client with a context: %s", err) 153 goto out 154 } 155 // Delete account. 156 err = client.DeleteUser(&gitkit.User{LocalID: u.ID}) 157 if err != nil { 158 aelog.Errorf(c, "Failed to delete user %v %v: %s", u.ID, u.Email, err) 159 goto out 160 } 161 // Account deletion succeeded. 162 // Call sign out to clear session and identity toolkit token. 163 aelog.Infof(c, "Account deletion succeeded") 164 165 handleSignOut(w, r) 166 return 167 out: 168 http.Redirect(w, r, successLandingURL, http.StatusFound) 169 }