示例#1
0
// CreateLoginToken creates a token to be used for login
func CreateLoginToken(familyID int, parentID int) (string, error) {
	exp := time.Now().Unix() + (3600 * 24 * 7)
	loginInfo := LoginTokenInfo{exp, familyID, parentID}

	payload, err := json.Marshal(loginInfo)
	if err != nil {
		panic(err)
	}
	token, err := jose.Sign(string(payload), jose.HS256, config.GetConfig().LoginKey)
	return string(token), err
}
示例#2
0
// OpenDB creates the DB object that will be used by everyone else
func OpenDB() error {
	var err error
	db, err = sql.Open("mysql", config.GetConfig().DbURL)
	if err != nil {
		return err
	}
	if err = db.Ping(); err != nil {
		log.Println("Startup ping failed: ", err)
	}
	return nil
}
示例#3
0
// ParseLoginToken takes a login token and confirms that it's valid
func ParseLoginToken(token string) (LoginTokenInfo, error) {
	payload, _, err := jose.Decode(token, config.GetConfig().LoginKey)
	response := new(LoginTokenInfo)

	if err == nil {
		err = json.Unmarshal([]byte(payload), response)
		if err == nil {
			if response.Expiration < time.Now().Unix() {
				return *response, RequestError{"Login token is expired"}
			}
			return *response, nil
		}
	}

	return *response, err
}