Example #1
0
File: token.go Project: xtfly/goman
func (ut *UserToken) GenToken(crypto *gokits.Crypto) (string, error) {
	bs := make([]byte, 0)
	buf := bytes.NewBuffer(bs)

	// flag
	buf.WriteByte(byte(ut.Flag))

	// uid
	binary.Write(buf, binary.BigEndian, ut.Uid)

	// client ip
	ip := net.ParseIP(ut.ClientIP)
	//log.Info("token length, ip  ", ut.ClientIP)
	buf.Write([]byte(ip.To4()))

	// current time
	binary.Write(buf, binary.BigEndian, time.Now().Unix())

	// expire
	binary.Write(buf, binary.BigEndian, ut.Expire)

	// encrypt
	//log.Info("token length, expire ", buf.Len())
	if t, err := crypto.Encrypt(buf.Bytes()); err != nil {
		return "", err
	} else {
		return base64.URLEncoding.EncodeToString(t), nil
	}
}
Example #2
0
// initialize the db driver and config
func InitDB(c *gokits.Crypto) {
	if dbinited {
		return
	}

	// 设置为 UTC 时间
	orm.DefaultTimeLoc = time.UTC
	orm.Debug = false

	web, err := macaron.Config().GetSection("web")
	if err != nil {
		panic(err)
	}
	webcfg = web

	sqllog := webcfg.Key("sqllog").String() // log the sql string
	if "on" == sqllog {
		orm.Debug = true
	}

	dbtype = web.Key("dbtype").String()
	log.Debugf("DB type is %s", dbtype)
	dbcfg, err := macaron.Config().GetSection(dbtype)
	if err != nil {
		panic(err)
	}

	switch dbtype {
	case "mysql":
		var username string = dbcfg.Key("username").String()
		if username, err = c.DecryptStr(username); err != nil {
			panic(err)
		}

		var password string = dbcfg.Key("password").String()
		if password, err = c.DecryptStr(password); err != nil {
			panic(err)
		}

		url := dbcfg.Key("url").String()
		maxidle = dbcfg.Key("maxidle").MustInt(2)
		maxconn = dbcfg.Key("maxconn").MustInt(2)
		dburl = username + ":" + password + "@" + url
		orm.RegisterDriver(dbtype, orm.DR_MySQL)
	case "sqlite":
		dburl = dbcfg.Key("url").String()
		dbtype = "sqlite3"
		orm.RegisterDriver(dbtype, orm.DR_Sqlite)
	}

	dbinited = true
}
Example #3
0
File: token.go Project: xtfly/goman
func (ut *UserToken) DecodeToken(crypto *gokits.Crypto, token string) bool {
	bs, err := base64.URLEncoding.DecodeString(token)
	if err != nil {
		return false
	}

	// decrypt
	var tbs []byte
	tbs, err = crypto.Decrypt(bs)
	if err != nil {
		log.Error("Decrypt failed ", err.Error())
		return false
	}

	// check the len
	if l := len(tbs); l != TokenLen {
		log.Error("Invalid length ", l)
		return false
	}

	ut.Flag = TokenFlag(tbs[0])
	// remain bytes
	buf := bytes.NewReader(tbs[1:])

	// read uid
	if err := binary.Read(buf, binary.BigEndian, &ut.Uid); err != nil {
		log.Error("Read uid failed ", err.Error())
		return false
	}

	// ip
	ip := make([]byte, 4)
	buf.Read(ip)
	ut.ClientIP = net.IP(ip).String()
	//log.Info("token ip  ", ut.ClientIP)

	// gen time
	if err := binary.Read(buf, binary.BigEndian, &ut.GenTime); err != nil {
		log.Error("Read generate time failed ", err.Error())
		return false
	}

	// expire time
	if err := binary.Read(buf, binary.BigEndian, &ut.Expire); err != nil {
		log.Error("Read expire time failed ", err.Error())
		return false
	}

	return true
}