// Will serve image, when user does not click reload // yet. func (rcv *controller) createNewImage(image string) error { c := redis.Get() secret, err := goredis.Bytes(c.Do("GET", image)) if err != nil { return err } png.Encode(rcv.response, captcha.NewImage(image, secret, captcha.StdWidth, captcha.StdHeight)) return nil }
// Need request object, for reading session id from request context func InsertData(r *http.Request, key string, value interface{}) { sid := context.Get(r, context.SID).(string) c := redis.Get() if _, err := c.Do("HMSET", sid, key, value); err != nil { panic(err.Error()) } // Session storage id will be remove in 10 hours. Keep // redis database clean. c.Do("EXPIREAT", sid, time.Now().Add(time.Hour*10).Unix()) }
// If value is not found, then return empty string func ReadData(r *http.Request, key string) (interface{}, error) { var value string sid := context.Get(r, context.SID).(string) c := redis.Get() raw, err := redigo.Values(c.Do("HMGET", sid, key)) redigo.Scan(raw, &value) if err != nil { return nil, err } return value, nil }
// Generate png filename and encode it. The encoded code will // will save on html and will by post request decoded, that // use to identify, if the user have enter the right captcha code. func Create() (string, string) { image := uniuri.NewLen(25) secret := captcha.RandomDigits(7) c := redis.Get() if _, err := c.Do("SET", image, secret); err != nil { panic(err.Error()) } if _, err := c.Do("EXPIRE", image, expired); err != nil { panic(err.Error()) } return image, base64.StdEncoding.EncodeToString([]byte(image)) }
// After every incoming request, refresh the time to live // of session, to ensure that user is still send request // to the server func (rcv *controller) renewTime(sid string) error { // Validate, if the session identification already exists in redis c := redis.Get() exists, err := redigo.Bool(c.Do("EXISTS", sid)) if err != nil { return err } if !exists { return nil } c.Do("EXPIREAT", sid, time.Now().Add(time.Minute*30).Unix()) return nil }
// Read activate id from url and validate if the id // can be activated. func (rcv *controller) read() error { id := mux.Vars(rcv.Request)["id"] con := redis.Get() // Get the saved id to activate from redis reply, err := goredis.Values(con.Do("HGETALL", id)) con.Do("DEL", id) if err != nil { return err } rcv.store = new(data) if err := goredis.ScanStruct(reply, rcv.store); err != nil { return err } return nil }
// After successfully signed up, it will send a confirmation // email to user with the to activated link. Redis will keep // this uri for 24 hours. If the user does not activated the // account within this time, it will deleted from neo4j data- // base and the user have to sign up again. func (rcv *controller) sendActivationLink(email string) error { uri := uniuri.NewLen(20) expired := time.Now().Unix() + 86400 con := redis.Get() _, err := con.Do("HMSET", uri, "email", email, "expired", expired) if err != nil { return err } // Will delete the link in 48 hours con.Do("EXPIREAT", uri, time.Now().Add(time.Hour*48).Unix()) link := rcv.Request.Host + "/activate/" + uri if err = mail.Send(email, link); err != nil { return err } return nil }
// Request new image, if the previous captcha is difficult to recognize. func (rcv *controller) changeImage(image string) { c := redis.Get() // Configure out, if the image still available. _, err := goredis.Bytes(c.Do("GET", image)) if err != nil { http.NotFound(rcv.response, rcv.request) return } secret := captcha.RandomDigits(7) if _, err := c.Do("SET", image, secret); err != nil { panic(err.Error()) } if _, err := c.Do("EXPIRE", image, expired); err != nil { panic(err.Error()) } png.Encode(rcv.response, captcha.NewImage(image, secret, captcha.StdWidth, captcha.StdHeight)) }
// Validate if the entered numbers match to stored number func Validate(r *http.Request, certification, human string) error { // Error object err := errors.New(i18n.Translate(httphead.GetLang(r), "controller/account", "text09")) if human == "" { return err } decoded, err := base64.StdEncoding.DecodeString(certification) if err != nil { return err } c := redis.Get() // Configure out, if the image still available. values, err := goredis.Bytes(c.Do("GET", string(decoded))) if err != nil { return err } ns := make([]byte, len(human)) for i := range ns { d := human[i] switch { case '0' <= d && d <= '9': ns[i] = d - '0' case d == ' ' || d == ',': // ignore default: return err } } if !bytes.Equal(values, ns) { return err } return nil }
// Build the reset link, that user can call page // to reset the password func (rcv *controller) buildLink() (string, error) { id := uniuri.NewLen(17) con := redis.Get() if _, err := con.Do("SET", id, rcv.formUser.Email); err != nil { return "", err } // The link will be deleted in 24 hours. After then, the user // have to reqeuest for changing password again. con.Do("EXPIREAT", id, time.Now().Add(time.Hour*24).Unix()) url, err := rcv.Router.Get("resetpw").URL("id", id) if err != nil { return "", err } link := rcv.Request.Host + url.String() return link, nil }
// Read email address from redis, that mapped to the // link id func (rcv *controller) readEmailAddr() (string, error) { id := mux.Vars(rcv.Request)["id"] conn := redis.Get() return goredis.String(conn.Do("GET", id)) }
func DeleteData(r *http.Request, key string) { sid := context.Get(r, context.SID).(string) c := redis.Get() c.Do("HDEL", sid, key) }