Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
const message = JSON.stringify({
signed_message: signedMessage,
authors: tokenObj.authors,
timestamp: tokenObj.timestamp,
})
let hash = cryptoUtils.sha256(message)
...
account[type].key_auths.forEach((key: string[]) => {
if (
!validSignature
&& PublicKey.fromString(key[0]).verify(hash, Signature.fromString(signature))
) {
validSignature = true;
}
});access_token and verifies if it was created by same app, in above instances, @hive.blog and @ecency.app, when you setting up your own instance of imagehoster, your users in your app will be able to verify and upload images to your instance. Below is code section to show how verification is done.https://hivesigner.com/oauth2/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=vote,commenthttps://example.com/callback?access_token=ACCESS_TOKEN&expires_in=36000curl -H "Authorization: ACCESS_TOKEN" https://hivesigner.com/api/mePOST https://hivesigner.com/api/broadcast
Authorization: ACCESS_TOKEN
Content-Type: application/json
Accept: application/json
Body: {
"operations": [
["vote", {
"voter": "guest123",
"author": "ecency",
"permlink": "trustpilot",
"weight": 10000
}]
]
}{
"errors": null,
"result": {
"ref_block_num": 32098,
"ref_block_prefix": 793145245,
"expiration": "2021-05-27T05:49:36",
"operations": [
["vote", {
"voter": "guest123",
"author": "ecency",
"permlink": "trustpilot",
"weight": 10000
}]
],
"extensions": [],
"signatures":
["205eeb64618343f0f3965a7292dde5a1be00defc31f1df8d103f0c8e8abcd36ff139c2db032549a0969f9abcd7aaffb4d69b8d67ef9d6d386572998c8b778f6f9e"]
}
} // take access token from url params
const token = ctx.params['accesstoken']
//decode access token
const decoded = Buffer.from(b64uToB64(token), 'base64').toString()
// parse it into object
const tokenObj = JSON.parse(decoded)
const signedMessage = tokenObj.signed_message
if (
tokenObj.authors
&& tokenObj.authors[0]
&& tokenObj.signatures
&& tokenObj.signatures[0]
&& signedMessage
&& signedMessage.type
&& ['login', 'posting', 'offline', 'code', 'refresh']
.includes(signedMessage.type)
&& signedMessage.app
) {
// get username from access_token
const username = tokenObj.authors[0]
let account = {
name: '',
reputation: 0,
}
// initialize Hivesigner with user access_token and app_account from imagehoster config
const cl = new hivesigner.Client({
app: UPLOAD_LIMITS.app_account,
accessToken: token,
})
await cl.me(function (err: any, res: any) {
if (!err && res) {
account = res.account
APIError.assert(account, APIError.Code.NoSuchAccount)
ctx.log.warn('uploading app %s', signedMessage.app)
APIError.assert(username === account.name, APIError.Code.InvalidSignature)
// user access_token should have same signed app account as imagehoster defined app account.
APIError.assert(signedMessage.app === UPLOAD_LIMITS.app_account, APIError.Code.InvalidSignature)
APIError.assert(res.scope.includes('comment'), APIError.Code.InvalidSignature)
// check if user has authorized posting authority to app_account
if (account && account.name) {
['posting', 'active', 'owner'].forEach((type) => {
account[type].account_auths.forEach((key: string[]) => {
if (
!validSignature
&& key[0] === UPLOAD_LIMITS.app_account
) {
validSignature = true;
}
});
});
}
}
});
}