Example #1
0
// Given the payload section of a JWS
// signature, it base64url decodes and returns
// a map structure representing the payload
func TokenToBlock(token string, blockName string) (map[string]interface{}, error) {

	var block = map[string]interface{}{}

	var payload, err = util.GetJWSPayload(token)
	if err != nil {
		return block, err
	}

	blockJSON, err := crypto.FromBase64Raw(payload)
	if err != nil {
		return block, errors.New("invalid " + blockName + " token")
	}

	block, err = util.JSONToMap(blockJSON)
	if err != nil {
		return block, errors.New("malformed " + blockName + " block")
	}

	return block, nil
}
Example #2
0
// Decode a base64 encoded stone token
func Decode(encStone string) (*Stone, error) {

	var stone = Empty()

	// decode encoded token to get the signatures
	sigJSON, err := crypto.FromBase64Raw(encStone)
	if err != nil {
		return &Stone{}, errors.New("failed to decode token")
	}

	// convert json to map
	stoneMap, err := util.JSONToMap(sigJSON)
	if err != nil {
		return &Stone{}, errors.New("failed to parse token")
	}

	// parse and load meta block
	if util.IsStringValue(stoneMap["meta"]) && stoneMap["meta"].(string) != "" {

		var token = stoneMap["meta"].(string)

		block, err := TokenToBlock(token, "meta")
		if err != nil {
			return stone, err
		}

		stone.Meta = block
		stone.Signatures["meta"] = token
	}

	// parse and load ownership block
	if util.IsStringValue(stoneMap["ownership"]) && stoneMap["ownership"].(string) != "" {

		var token = stoneMap["ownership"].(string)

		block, err := TokenToBlock(token, "ownership")
		if err != nil {
			return stone, err
		}

		stone.Ownership = block
		stone.Signatures["ownership"] = token
	}

	// parse and load ownership block
	if util.IsStringValue(stoneMap["attributes"]) && stoneMap["attributes"].(string) != "" {

		var token = stoneMap["attributes"].(string)

		block, err := TokenToBlock(token, "attributes")
		if err != nil {
			return stone, err
		}

		stone.Attributes = block
		stone.Signatures["attributes"] = token
	}

	// parse and load embeds block
	if util.IsStringValue(stoneMap["embeds"]) && stoneMap["embeds"].(string) != "" {

		var token = stoneMap["embeds"].(string)

		block, err := TokenToBlock(token, "embeds")
		if err != nil {
			return stone, err
		}

		stone.Embeds = block
		stone.Signatures["embeds"] = token
	}

	return stone, nil
}