//Auth for client func authClient(token string) (string, error) { var result interface{} db.MgoSession().SetMode(mgo.Monotonic, true) _, err := db.MgoSession().DB("pushd").C("token").Find(bson.M{"tk": token}).Select(bson.M{"_id": 0, "tk": 1}).Apply(mgo.Change{Remove: true}, &result) if err == mgo.ErrNotFound { return "", errors.New("Client auth fail") } else if err != nil { log.Error("get token from db error: %s", err.Error()) return "", errors.New("Client auth fail") } return "Client auth succeed", nil }
// TODO more secure token generator func getToken() (token string) { token = str.Rand(32) err := db.MgoSession().DB("pushd").C("token").Insert(bson.M{"tk": token, "expire": time.Now()}) if err != nil { log.Error("generate token error: %s", err.Error()) return "" } return }
func (this *MongoStorage) storeMulti(mts []*MsgTuple) error { records := make([]interface{}, 0) for _, mt := range mts { records = append(records, bson.M{"channel": mt.Channel, "msg": mt.Msg, "ts": mt.Ts}) } err := db.MgoSession().DB("pushd").C("msg_log").Insert(records...) return err }
func (this *MongoStorage) fetchByChannelAndTs(channel string, ts int64) (result []interface{}, err error) { if channel == "" { return nil, errors.New("No channel specified") } err = db.MgoSession().DB("pushd").C("msg_log").Find(bson.M{"ts": bson.M{"$gte": ts}, "channel": channel}).Select(bson.M{"_id": 0}).All(&result) if err != nil && err != mgo.ErrNotFound { log.Error("fetch messages log from db error: %s", err.Error()) } if len(result) == 0 { return []interface{}{}, nil } return }
func authServer(appId, secretKey string) (string, error) { c := db.MgoSession().DB("pushd").C("user") var result interface{} err := c.Find(bson.M{"appId": appId}).One(&result) if err == mgo.ErrNotFound { return "", errors.New("Server auth fail") } else if err != nil { log.Error("Error occured when query mongodb: %s", err.Error()) } key := result.(bson.M)["secretKey"] if key == secretKey { return "Server auth succeed", nil } return "", errors.New("Server auth fail") }
func (this *MongoStorage) store(mt *MsgTuple) error { err := db.MgoSession().DB("pushd").C("msg_log").Insert(bson.M{"channel": mt.Channel, "msg": mt.Msg, "ts": mt.Ts}) return err }