Repository: codenotary/immudb
File: pkg/client/token_service.go (Go)

Code before:
//IsTokenPresent ...
func (ts *tokenService) IsTokenPresent() (bool, error) {
	return ts.hds.FileExistsInUserHomeDir(ts.tokenFileName)
}

func (ts *tokenService) GetDatabase() (string, error) {
	dbname, _, err := ts.parseContent()
	return dbname, err
}

func (ts *tokenService) parseContent() (string, string, error) {
	content, err := ts.hds.ReadFileFromUserHomeDir(ts.tokenFileName)
	if err != nil {
		return "", "", err
	}
	if len(content) <= 8 {
		return "", "", errors.New("token content not present")
	}
	// token prefix is hardcoded into library. Please modify in case of changes in paseto library
	if strings.HasPrefix(content, "v2.public.") {
		return "", "", errors.New("old token format. Please remove old token located in your default home dir")
	}
	dbnamelen := make([]byte, 8)
	copy(dbnamelen, content[:8])
	dbnamelenUint64 := binary.BigEndian.Uint64(dbnamelen)
	if int(dbnamelenUint64) > len(content) {
		return "", "", errors.New("invalid token format")
	}
	databasename := make([]byte, int(dbnamelenUint64))
	copy(databasename, content[8:8+int(dbnamelenUint64)])

	token := make([]byte, len(content)-8-int(dbnamelenUint64))
	copy(token, content[8+int(dbnamelenUint64):])

	return string(databasename), string(token), nil
}

// WithHds ...
func (ts *tokenService) WithHds(hds HomedirService) TokenService {
	ts.hds = hds
	return ts
}

// WithTokenFileName ...
func (ts *tokenService) WithTokenFileName(tfn string) TokenService {
	ts.tokenFileName = tfn
	return ts
}


Review comment (suggestion):
yes, actually should not be greater than remaining content, 8 bytes were already consumed when reading the encoded length

Code after:
//IsTokenPresent ...
func (ts *tokenService) IsTokenPresent() (bool, error) {
	return ts.hds.FileExistsInUserHomeDir(ts.tokenFileName)
}

func (ts *tokenService) GetDatabase() (string, error) {
	dbname, _, err := ts.parseContent()
	return dbname, err
}

func (ts *tokenService) parseContent() (string, string, error) {
	content, err := ts.hds.ReadFileFromUserHomeDir(ts.tokenFileName)
	if err != nil {
		return "", "", err
	}
	if len(content) <= 8 {
		return "", "", errors.New("token content not present")
	}
	// token prefix is hardcoded into library. Please modify in case of changes in paseto library
	if strings.HasPrefix(content, "v2.public.") {
		return "", "", errors.New("old token format. Please remove old token located in your default home dir")
	}
	dbNameLen := make([]byte, 8)
	copy(dbNameLen, content[:8])
	dbNameLenUint64 := binary.BigEndian.Uint64(dbNameLen)
	if dbNameLenUint64 > uint64(len(content))-8 {
		return "", "", errors.New("invalid token format")
	}
	databasename := make([]byte, dbNameLenUint64)
	copy(databasename, content[8:8+dbNameLenUint64])

	token := make([]byte, uint64(len(content))-8-dbNameLenUint64)
	copy(token, content[8+dbNameLenUint64:])

	return string(databasename), string(token), nil
}

// WithHds ...
func (ts *tokenService) WithHds(hds HomedirService) TokenService {
	ts.hds = hds
	return ts
}

// WithTokenFileName ...
func (ts *tokenService) WithTokenFileName(tfn string) TokenService {
	ts.tokenFileName = tfn
	return ts
}