Protobuf decoding uint64 invalid in Chrome Extension - javascript

I'm trying to get decrypted information from the Steam using protobuf, but since it contains a uint64, the javascript immediately writes it as a number into the decrypted json and, due to its length, this number turns into a rounded number. I do not understand how to decrypt the protobuff message so that instead of a crooked number, get the correct string with uint64 data.
Response proto:
"CAuthentication_BeginAuthSessionViaCredentials_Response": {
"fields": {
"client_id": {
"type": "uint64",
"id": 1,
"jstype": "JS_STRING",
"options": {"jstype": "JS_STRING", "(description)": "unique identifier of requestor, also used for routing"}
},
"request_id": {
"type": "bytes",
"id": 2,
"options": {"(description)": "unique request ID to be presented by requestor at poll time - must not be transferred or displayed"}
},
"interval": {
"type": "float",
"id": 3,
"options": {"(description)": "refresh interval with which requestor should call PollAuthSessionStatus"}
},
"allowed_confirmations": {
"rule": "repeated",
"type": ".CAuthentication_AllowedConfirmation",
"id": 4,
"options": {"(description)": "the confirmation types that will be able to confirm the request"}
},
"steamid": {
"type": "uint64",
"id": 5,
"options": {"jstype": "JS_STRING", "(description)": "steamid of the account logging in - will only be included if the credentials were correct"}
},
"weak_token": {
"type": "string",
"id": 6,
"options": {"(description)": "partial-authentication token - limited lifetime and scope, included only if credentials were valid"}
}
}
}
Site response in decode:
{
...
steamid: 76561198140356100
...
}
Original data is 76561198140356099 , but JS Convert it to 76561198140356100.
Binary for example:
new Uint8Array([10,128,4,97,97,98,49,56,52,100,49,55,50,48,102,53,98,99,52,98,49,52,51,102,48,57,56,102,99,51,49,56,48,51,101,101,100,102,50,101,52,100,99,48,100,55,50,56,102,97,101,53,53,51,53,55,55,50,56,53,99,51,57,102,52,48,100,55,101,51,57,52,57,101,101,98,97,50,52,100,54,97,53,54,57,53,49,52,52,98,49,102,98,100,100,52,56,55,49,51,56,49,54,54,100,101,98,53,54,102,50,102,53,48,49,101,54,52,53,97,57,52,50,97,54,102,49,49,101,53,50,100,97,50,57,98,52,98,102,54,99,54,48,98,102,102,48,52,50,50,52,49,97,97,53,55,100,97,99,99,48,98,100,102,56,97,98,49,56,55,51,49,52,48,48,48,102,53,50,48,54,102,99,57,102,50,48,52,97,55,97,50,100,98,56,52,57,97,98,101,57,57,51,48,101,57,56,55,48,49,54,97,100,52,102,56,57,101,52,49,54,50,48,98,55,102,56,102,57,99,97,48,52,99,49,98,100,98,49,52,57,56,99,49,57,101,53,98,54,100,55,48,50,55,100,51,98,54,102,50,52,48,55,53,50,56,97,102,56,98,99,100,49,51,102,50,48,98,51,48,97,56,55,102,99,100,55,50,51,101,53,101,101,55,52,99,48,55,54,102,101,100,101,99,57,99,56,99,55,52,54,56,50,55,56,53,98,54,55,55,101,100,52,100,50,100,99,56,56,56,54,98,57,98,54,51,56,99,56,97,54,48,54,97,48,51,49,99,98,100,53,100,48,57,98,99,101,52,53,99,53,48,99,48,57,100,99,55,98,50,53,50,97,51,97,100,57,100,53,52,99,99,55,52,52,49,49,100,50,55,51,57,52,98,52,97,54,101,53,54,54,53,53,51,57,49,102,57,54,54,52,52,48,99,51,56,100,99,102,48,97,55,52,50,97,49,102,57,55,101,101,98,100,99,53,56,50,55,101,50,54,57,97,102,55,100,52,48,99,99,102,99,54,56,55,99,57,99,52,100,50,56,97,56,55,99,54,49,54,55,100,51,55,55,97,48,98,100,55,56,57,54,49,48,98,99,98,49,102,57,57,99,48,54,97,52,49,55,56,57,57,99,55,100,50,49,53,102,100,98,101,55,53,55,49,18,6,48,49,48,48,48,49,24,240,200,254,191,132,16]).buffer;
Proto for exampled buffer:
"CAuthentication_GetPasswordRSAPublicKey_Response": {
"fields": {
"publickey_mod": {
"type": "string",
"id": 1,
"options": {"(description)": "the public key modulus"}
},
"publickey_exp": {"type": "string", "id": 2, "options":{"(description)": "the public key exponent"}},
"timestamp": {
"type": "uint64",
"id": 3,
"options": {"jstype": 1, "(description)": "the timestamp the key was generated"}
}
}
}
To solve the problem, you need to get a json object from the buffer data in which the time stamp value will not be a number, but a string.

Related

How can I get all the name parts of all the second choices in a json object

This is the full object that I want to get something from is:
{
"name": "permissions",
"description": "Get or edit permissions for a user or a role",
"options": [
{
"name": "user",
"description": "Get or edit permissions for a user",
"type": 2,
"options": [
{
"name": "get",
"description": "Get permissions for a user",
"type": 1,
"options": [
{
"name": "user",
"description": "The user to get",
"type": 6,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to get. If omitted, the guild permissions will be returned",
"type": 7,
"required": false
}
]
},
{
"name": "edit",
"description": "Edit permissions for a user",
"type": 1,
"options": [
{
"name": "user",
"description": "The user to edit",
"type": 6,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to edit. If omitted, the guild permissions will be edited",
"type": 7,
"required": false
}
]
}
]
},
{
"name": "role",
"description": "Get or edit permissions for a role",
"type": 2,
"options": [
{
"name": "get",
"description": "Get permissions for a role",
"type": 1,
"options": [
{
"name": "role",
"description": "The role to get",
"type": 8,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to get. If omitted, the guild permissions will be returned",
"type": 7,
"required": false
}
]
},
{
"name": "edit",
"description": "Edit permissions for a role",
"type": 1,
"options": [
{
"name": "role",
"description": "The role to edit",
"type": 8,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to edit. If omitted, the guild permissions will be edited",
"type": 7,
"required": false
}
]
}
]
}
]
}
I want to get all of these name attributes from the first json object and assign them all to different variables in javascript? How can I achieve this?
Permissions ---> role ---> edit ---> channel (In other words I want to get all of the name parts in all the second choices in the json object)
If you know that JSON structure is fixed then it will be easy.
Consider your json is assigned to permissionJson then you can write it as
permissionJson.name // permissions
permissionJson.options[1].name. // role
permissionJson.options[1].options[1].name // edit
permissionJson.options[1].options[1].options[1].name // channel
if JSON structure is dynamic then you might need loops to extract the options array index 1 element at each level and find its name.
first of all i want to make clear that i don't understand your question very well. So correct me if i misunderstood something.
you said you want to get all name parts in all second member of options(root one) array. so all you have to do is
JSON_DATA.options[1]
Here you have selected whole role object. to get more deeper you can use dot operator. For instance Permissions ---> role ---> edit ---> channel. this could be accessed by this line JSON_DATA.options[1].options[1]
it will return this
{
"name": "channel",
"description": "The channel permissions to get. If omitted, the guild permissions will be returned",
"type": 7,
"required": false
}
This solution works generic for as deep as the object might get in the future
This is the solution for you: Just use this function, and create the variable allSecondNames:
const allSecondNames = [];
func = a => {
if (a.name) allSecondNames.push(a.name);
if (a.options) func(a.options)
if (a[1] && a[1].name) allSecondNames.push(a[1].name);
if (a[1]?.options) func(a[1].options)
}
Sending your JSON to function func would put in variable allSecondNames:
["permissions", "role", "edit", "channel"]

How can I access all the "text" properties of the json of cucumber's "step"-object and log them to the console?

I'm trying to log the string/regex of a step description of a cucumber 'step'-oject to the console. This is an example step
Given Alice is hungry
... and this is the first line to the step definition
Given( /^Alice is hungry$/, () => {
I'm trying to log 'Given Alice is hungry' to console, using the string representation of a 'step'-object that's provided as an argument, in the context of a cucumber-specific hook of webdriverio. Running
beforeStep: function ({ uri, feature, step }, context) {
console.log(`Running "${JSON.stringify(step, null, 4)}"`);
}
...produces this output:
[0-0] Running "{
"uri": "featureFiles\\dev\\my-first-feature-file.feature",
"feature": {
"type": "Feature",
"tags": [],
"location": {
"line": 8,
"column": 1
},
"language": "en",
"keyword": "Functionality",
"name": "Eating too many cucumbers is not good for you",
"children": [
{
"type": "Scenario",
"tags": [
{
"type": "Tag",
"location": {
"line": 10,
"column": 3
},
"name": "#Szenario-Eating-all-the-cucumbers"
}
],
"location": {
"line": 11,
"column": 3
},
"keyword": "Szenario",
"name": "Eating a few is no problem",
"steps": [
{
"type": "Hook",
"location": {
"line": 187,
"column": 0,
"uri": "node_modules\\#wdio\\cucumber-framework\\build\\index
.js"
},
"keyword": "Hook",
"text": ""
},
{
"type": "Step",
"location": {
"line": 12,
"column": 3
},
"keyword": "Given",
"text": "Alice is hungry"
},
{
"type": "Step",
"location": {
"line": 13,
"column": 5
},
"keyword": "When ",
"text": "she eats 3 cucumbers'"
},
{
"type": "Step",
"location": {
"line": 14,
"column": 5
},
"keyword": "Then ",
"text": "she will be full"
},
However, when I use
beforeStep: function ({ uri, feature, step }, context) {
// eslint-disable-next-line no-undef
console.log(\`Running step "${step.text}"`);
}
...all i get is
[0-0] Running "undefined"
[0-0] Running "undefined"
I've tried these 2 options instead:
console.log(`Running "${step.feature.children.steps.text}"`);
console.log(`Running "${feature.children.steps.text}"`);
In both cases this produces the following result:
[0-0] Error in "BeforeStep Hook"
Cannot read property 'text' of undefined
What am I doing wrong?
Let's take this example:
${step.feature.children.steps.text}
children is an array, so you can't just type .steps. You need to access a certain element first, like [0].steps.
Similarly with steps, steps is an array:
"steps": [ ... ]
This ${step.text} doesn't work because step doesn't have a property text. It has the following keys:
uri
feature
I accepted pavelsman answer as the problem's solution.
For anyone who wants to log the cucumber steps to the console right when they're executing, just add this to the wdio-configuration file:
let scenarioIndex = 0;
let stepIndex = 0;
beforeStep: function ({uri, feature, step}, context) {
if(typeof step.feature.children[scenarioIndex] !== "undefined") {
let keyword = step.feature.children[scenarioIndex].steps[stepIndex].keyword;
let stepText = step.feature.children[scenarioIndex].steps[stepIndex].text;
if (stepText !== "") {
console.log(keyword + stepText);
}
stepIndex++;
}
},
afterScenario: function () {
scenarioIndex++;
stepIndex = 0;
},

Generate an output JSON message from an input JSON message based on an output JSON schema

I have a requirement to write a NodeJS solution and library to receive an input JSON data which the solution must transform into an output JSON data that matches or validates to a provided output JSON schema.
At the moment, I have an input file that validates to the destination JSON schema but has some missing fields and default values that the business rules permit as part of the transformation.
The source JSON is as follows:
{
"messageContext" : {
"statement" : "mediaDelivered",
"services" :[
"mss"
],
"domain":"my.xxx.yyy.create",
"function":"FCG"
},
"header" : {
"primaryEntityIDs":[
"my:png::abcxxxx"
],
"origin" : "my-png",
},
"data" :{
"operation": "CREATE",
"payload": {
"s3": "https://wildwildwest.amazonaws.com/png/myimg.mov"
}
}
}
The JSON schema that the generated output JSON must match or validate to is as follows:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://schema.example.com/v1-0-0/schema.json",
"definitions": {
"header": {
"type": "object",
"title": "Message headers",
"description": "Header properties for the message.",
"required": [
"serializedVersion",
"correlationID",
"projectID",
"sourceMessageID",
"eventTimestamp",
"messageType",
"primaryEntityIDs"
],
"additionalProperties": false,
"properties": {
"serializedVersion": {
"type": "string",
"title": "serialized Version",
"description": "The version of the schema to validate this message against, the SchemaVer convention.",
"default": "1-0-0",
"pattern": "^([1-9][0-9]*)-(0|[1-9][0-9]*)-(0|[1-9][0-9]*)$",
"examples": [
"1-0-0"
]
},
"correlationID": {
"type": "string",
"title": "Correlation ID",
"description": "Use this field to pass on the ID from an upstream message. If you do not have an upstream message, please create a guid.",
"examples": [
"4fd5217e-a3d8-4ffc-8b8c-76ea9ba0ccdb"
]
},
"projectID": {
"type": "string",
"title": "Project ID",
"description": "An ID for the current project, which is a grouping of messages that apply to a specific business context. For example: a commission, creating a new content version, migrating programmes to a new brand.",
"examples": [
"4fd5217e-a3d8-4ffc-8b8c-76ea9ba0ccdb"
]
},
"sourceMessageID": {
"type": "string",
"title": "Source Message ID",
"description": "A unique ID for this message. Generated by the system that generates the message.",
"examples": [
"6b659d8f-bf4b-4dbe-8d31-5c6e9b8553b6"
]
},
"eventTimestamp": {
"type": "string",
"title": "Event Timestamp",
"description": "The ISO 8601 datetime at which this message was created. Generated by the system that generates the message.",
"examples": [
"2015-07-09T10:45:01.555Z"
],
"format": "date-time"
},
"expiryTime": {
"type": "string",
"title": "Expiry Time",
"description": "The ISO 8601 datetime at which the message is no longer applicable. For a status message this would mean that the status message should be ignored after this time and the information in it considered no longer true. Timezone must be provided. Accuracy greater than 1s is not supported, greater accuracy will be truncated. If not provided time for hh, mm, or ss defaults to 00.",
"examples":[
"2015-07-09T10:45:01.555Z"
],
"format": "date-time"
},
"origin": {
"type": "string",
"title": "origin",
"description": "The specific system that generated this message. This field is for information only, and should not be coupled to. To understand the source of a message, please use messageContext/Function. This allows for looser coupling between systems and for multiple systems to provide the same function.",
"examples": [
"media-selector"
]
},
"messageType": {
"type": "string",
"title": "Message Type",
"description": "The type of serialized message.",
"enum": [
"STATUS",
"COMMAND",
"ERROR"
],
"examples": [
"STATUS"
]
},
"inResponseTo": {
"type": "string",
"title": "In Response To",
"description": "The message ID that this message is in response to.",
"examples": [
"6b659d8f-bf4b-4dbe-8d31-5c6e9b8553b6"
]
},
"primaryEntityIDs": {
"type": "array",
"title": "Primary Entity IDs",
"description": "A list of equivalent IDs (preferably URIs) that systems use to describe the content this message is about. For example, this may contain a What's On UID, and a PEEPS version ID. Any further entity identifiers can be in the Payload section (for example the identifier of an availability document).",
"items": {
"type": "string",
"title": "ID",
"description": "IDs (preferably URIs) for my content identifiers.",
"examples": [
"urn:my:CMM1:uid:ABC123E/01",
"urn:my:DST1:pid:p00123"
]
}
}
}
},
"messageContext": {
"type": "object",
"title": "Message Context",
"description": "The message context, combined with the Content-IDs section should contain all of the information needed to understand the purpose of this message.",
"required": [
"domain",
"function",
"statement"
],
"additionalProperties": false,
"properties": {
"services": {
"type": "array",
"title": "Services",
"description": "A list of services that this message is relevant to. For example, the editorial description of a piece of content may be approved only for on-platform services, such as mediaplayer.",
"items": {
"type": "string",
"title": "A my service",
"examples": [
"mediaplayer",
"my-plus",
"twitter"
]
}
},
"domain": {
"type": "string",
"title": "Domain",
"description": "The top level address of the function sending the message. This follows a dot notation, starting with the company (e.g. my), moving down a domain hierarchy.",
"examples": [
"my.content.distribution"
]
},
"function": {
"type": "string",
"title": "Function",
"description": "The business function sending the message. This is the generic function that the sending system is providing when sending this message. For example, Media Selector is providing the function of AvailabilityManagement, within the distribution domain. One system may have grown to provide more than one function, in which case the specific function that applies to this message should be used.",
"examples": [
"availabilityManagement"
]
},
"contentTypes": {
"type": "array",
"title": "Content Types",
"description": "The content type that this message applies to. In the AvailabilityManagement example, this could separately message the availability of media, or document data separately, or together.",
"items": {
"type": "string",
"enum": [
"MEDIA",
"DOCUMENT"
],
"examples": [
"MEDIA"
]
}
},
"statement": {
"type": "string",
"title": "Statement",
"description": "The string statement being made by the message which describes the event. This must be past-tense.",
"pattern": "(^\\S+$)",
"examples": [
"receivedMedia",
"linkedToPlanningItem",
"renditionCreated"
]
}
}
},
"data": {
"type": "object",
"title": "Data",
"description": "Dictionary describing the type of data being transferred and the transfer method. Includes either a reference to a data entity or an entity itself in a JSON document.",
"additionalProperties": false,
"properties": {
"operation": {
"type": "string",
"title": "Operation",
"description": "The CRUD operation that was performed.",
"enum": [
"CREATE",
"READ",
"UPDATE",
"DELETE"
]
},
"entitySchema": {
"type": "string",
"title": "Entity Schema",
"description": "The schema used for the object in the Payload.",
"examples": [
"http://url.my.dev/package.schema.json"
]
},
"entityType": {
"type": "string",
"title": "Entity Type",
"description": "The type of entity being provided in the Payload.",
"examples": [
"package"
]
}
},
"anyOf": [
{
"required": [
"payload"
],
"properties": {
"payload": {
"type": "object",
"title": "Payload",
"description": "The data entity being transferred. If both a reference and a payload are provided, then the objects in both must be identical.",
"properties": {},
"additionalProperties": true
}
}
},
{
"required": [
"reference"
],
"properties": {
"reference": {
"type": "string",
"title": "Reference",
"description": "A reference to the data entity being transferred. If both a reference and a payload are provided, then the objects in both must be identical.",
"examples": [
"https://url.my.dev/3b0f45d6-9051-4939-974f-8cbb7774b1db"
]
}
}
}
]
}
}
}
The rules are as follows:
I have been digging about and would like to know your thoughts on the best approach and Node libraries for solving this problem in NodeJS Javascript.
I found the Joi library to be a direct fit for my use case. I have tested and used it and it worked for me and gave me the need control to enforce the schema's constraints.
https://www.npmjs.com/package/joi
Another library the might be useful is https://www.npmjs.com/package/is-my-json-valid. However, I have not tested this library in depth.

trouble while parsing json

I am trying to react displayName in req
when I write
console.log(req.session.passport.user._raw)
the output is:
{
"kind": "plus#person",
"etag": "\"ucaTEV-ZanNH5M3SCxYRM0QRw2Y/XiR7kPThRbzcIw-YLiARoF22TMY\"",
"emails": [
{
"value": "rajanlagah#gmail.com",
"type": "account"
}
],
"objectType": "person",
"id": "100428644453494579140",
"displayName": "Rajan Lagah",
"name": {
"familyName": "Lagah",
"givenName": "Rajan"
},
"url": "https://plus.google.com/100428644453494579140",
"image": {
"url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
"isDefault": true
},
"isPlusUser": true,
"language": "en",
"circledByCount": 0,
"verified": false
}
Now the from this object I am trying to get kind (say)
console.log(req.session.passport.user._raw.kind)
then the output is undefined
Can any one tell my mistake?
Is req.session.passport.user._raw a string? You may need to parse it first, eg:
var data = JSON.parse(req.session.passport.user._raw);
console.log(data.kind);

TypeError: Object [object Array] has no method 'getProperty'

I'm kind of new to this whole posting a question thing, so please be gentle!
I am using Breeze as part of the "Hot Towel" SPA stack and retrieving data from a custom WebApi endpoint written in PHP. It's not a full implementation, I've just written enough to give me what I need.
The metadata my endpoint is generating is as follows:
{
"shortName": "Project",
"namespace": "WebApi.ORM.Cartesius",
"autoGeneratedKeyType": "Identity",
"defaultResourceName": "Project",
"dataProperties": [
{
"name": "id",
"isPartOfKey": true,
"isNullable": false,
"dataType": "Int32"
},
{
"name": "title",
"isNullable": false,
"maxLength": 256,
"dataType": "String"
},
{
"name": "date",
"isNullable": false,
"dataType": "DateTime"
},
{
"name": "review_date",
"isNullable": true,
"dataType": "DateTime"
},
{
"name": "summary",
"isNullable": true,
"dataType": "String"
}
],
"navigationProperties": [
{
"name": "Team",
"entityTypeName": "Team:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "team_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
{
"name": "ProjectAuthor",
"entityTypeName": "ProjectAuthor:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "project_author_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
{
"name": "Itinerary",
"entityTypeName": "Itinerary:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "itinerary_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
Everything is working fine until I try and do an expand on my query:
var query = new breeze.EntityQuery()
.from("Project")
.where("id","eq",project.id)
.expand("ProjectAuthor");
This query returns the following from my endpoint:
[
{
"$id": 1,
"$type": "WebApi.ORM.Cartesius.Project",
"id": 2,
"title": "teat",
"date": "2013-11-04 14:00:00+07",
"review_date": null,
"summary": null,
"ProjectAuthor": [
{
"$id": 2,
"$type": "WebApi.ORM.Cartesius.ProjectAuthor",
"id": 1,
"account_id": 1,
"project_id": 2,
"Project": [
{
"$ref": 1
}
]
},
{
"$id": 3,
"$type": "WebApi.ORM.Cartesius.ProjectAuthor",
"id": 3,
"account_id": 2,
"project_id": 2,
"Project": [
{
"$ref": 1
}
]
}
]
}
]
Then Breeze chokes on it throwing:
TypeError: Object [object Array] has no method 'getProperty'
Debugging points to line 5059 in Breeze where it tries to getProperty on an array of entities rather than a single entity I assumed this had something to do with whether the navigation property was set to scalar or not but switching them around made no difference.
I'm sure I'm doing something massively wrong, but I can't figure out what it is and I've kind of hit a brick wall. I've read the docs from top to bottom and am implementing this as best as I can understand but it's possible I'm a little confused.
Thanks in advance for any help and apologies if I have not made myself clear or provided enough info
I think your problem is in the metadata
"name": "ProjectAuthor",
"entityTypeName": "ProjectAuthor:#WebApi.ORM.Cartesius",
"isScalar": true,
"associationName": "project_author_project_id_fkey",
"invForeignKeyNames": [
"project_id"
]
},
On that nav property Project->ProjectAuthor your telling it that its a scalar but its not in the data set..
"ProjectAuthor": [
{
..........
I've had this problem aswell .. 99% time its a metadata problem, if not there then start cutting metadata apart until you isolate the error .. though I'm sure thats the problem.. you need a hasMany there and an isScalar on the inverse navigation

Categories

Resources