メモです
type tokenKey int
const (
ctxTest tokenKey = 0
)
type App struct {
Cache *cache.Cache
}
// uuidでtoken作成
func makeCsrfToken() string {
h := md5.New()
io.WriteString(h, uuid.New().String())
token := fmt.Sprintf("%x", h.Sum(nil))
return token
}
func (a *App) csrf(r *http.Request) {
token := makeCsrfToken()
ctx := context.WithValue(r.Context(), ctxTest, token)
r.WithContext(ctx)
a.Cache.Set(token, struct{}{}, cache.DefaultExpiration)
}
func (a *App) Authorize(w http.ResponseWriter, r *http.Request) {
...
...
a.csrf(r)
// 取り出す
csrfToken := r.Context().Value(ctxTest).(string)
}