Skip to content

Instantly share code, notes, and snippets.

View SerKnight's full-sized avatar
🏄‍♂️

Christopher Knight SerKnight

🏄‍♂️
View GitHub Profile
@SerKnight
SerKnight / example_token_set.json
Last active June 5, 2020 19:57
OAuth2.0 token set
{
"access_token":"xxxxxx.xxxxxx.xxxxxx-xxxxx-xxxxx-xxxxx",
"refresh_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"expires_in":"1800",
"token_type":"Bearer",
"xero_tenant_id":"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
}
@SerKnight
SerKnight / variable-setup.rb
Last active June 4, 2020 20:07
OAuth migration tutorial - 0
require 'securerandom'
TOKEN = 'VALID_OAUTH_10A_ACCESS_TOKEN'
CONSUMER_KEY = 'YOUR_CONSUMER_KEY'
SCOPES = 'accounting.transactions offline_access'
OAUTH2_CLIENT_ID = 'YOUR_OAUTH20_CLIENT_ID'
OAUTH2_CLIENT_SECRET = 'YOUR_OAUTH20_CLIENT_SECRET'
SIGNATURE_METHOD = 'RSA-SHA1'
ENDPOINT = 'https://api.xero.com/oauth/migrate'
@SerKnight
SerKnight / token-migration.rb
Last active June 4, 2020 21:24
OAuth migration tutorial - 5
require 'uri'
require 'json'
require 'net/http'
uri = URI.parse(ENDPOINT)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = {'Content-Type' =>'application/json', 'Authorization': authorization_header}
request = Net::HTTP::Post.new(uri.request_uri, headers)
@SerKnight
SerKnight / signature-base-string.rb
Last active June 5, 2020 18:27
OAuth migration tutorial - 4
require 'uri'
signature_base_string = "POST&" + # Uppercase HTTP method
"#{URI.encode_www_form_component(ENDPOINT)}&" + # Base URI
URI.encode_www_form_component(base_params).to_s # OAuth parameter string
@SerKnight
SerKnight / sign_sbs_with_private_key.rb
Created June 3, 2020 20:37
OAuth migration tutorial - 3
require 'uri'
require 'base64'
require 'openssl'
require 'digest/sha1'
signing_key = File.read('./privatekey.pem')
rsa_key = OpenSSL::PKey::RSA.new signing_key
digest = OpenSSL::Digest::SHA1.new
signature = rsa_key.sign(digest, signature_base_string)
@SerKnight
SerKnight / setup-base-params.rb
Last active June 5, 2020 19:30
OAuth migration tutorial - 2
base_params = "oauth_consumer_key=#{CONSUMER_KEY}" +
"&oauth_nonce=#{NONCE}" +
"&oauth_signature_method=#{SIGNATURE_METHOD}" +
"&oauth_timestamp=#{TIMESTAMP}" +
"&oauth_token=#{TOKEN}" +
"&oauth_version=1.0" +
"&tenantType=ORGANISATION"
@SerKnight
SerKnight / authorization-headers-and-body.rb
Last active June 8, 2020 22:46
OAuth migration tutorial - 1
params = {
"scope": "offline_access accounting.transactions accounting.settings",
"client_id": "YOUR_OAUTH2_CLIENT_ID",
"client_secret": "YOUR_OAUTH2_CLIENT_SECRET"
}
@SerKnight
SerKnight / authorization-headers-and-body.rb
Created June 3, 2020 20:29
Xero Migration Tutorial - 1
authorization_headers = "OAuth oauth_consumer_key='#{CONSUMER_KEY}',
oauth_nonce='#{NONCE}',
oauth_signature_method='#{SIGNATURE_METHOD}',
oauth_timestamp='#{TIMESTAMP}',
oauth_token='#{TOKEN}',
oauth_version='1.0',
tenantType='ORGANISATION',
oauth_signature='#{oauth_signature}'".gsub("\n",' ')
=> "OAuth oauth_consumer_key='YOUR_CONSUMER_KEY', oauth_nonce='1cbf3d69-d478-4956-b574-a4c6c4a4b2c4', oauth_signature_method='RSA-SHA1', oauth_timestamp='1591214409', oauth_token='VALID_OAUTH_10A_ACCESS_TOKEN', oauth_version='1.0', tenantType='ORGANISATION', oauth_signature='dkkQVrBsTfWqCatt4AmInAtee3Aitmje5jtjjoWl0HZl%2BuriiCjY%2Fe%2FgM9M9XW75DeRXX1GYpeiJI6B0ogGylF9myRTv6KhDpEpiFe5tdaJ0b2LdcWbODHhLP98%f4LKCJVPaS9Y6atX8734qDx1z0hLhVREIDtNFEb%2BpyTpgejeI%3D'"
@SerKnight
SerKnight / Insomnia_workspace.json
Created February 3, 2020 22:22
Insomnia workspace to accompany Xero API tutorial
{
"_type":"export",
"__export_format":4,
"__export_date":"2020-02-03T22:17:29.587Z",
"__export_source":"insomnia.desktop.app:v7.0.6",
"resources":[
{
"_id":"req_1a56b9b120a94264b79018d1ecee95f3",
"authentication":{
"accessTokenUrl":"https://identity.xero.com/connect/token",
@SerKnight
SerKnight / FaceBlock.ts
Last active March 18, 2019 04:49
Example smart contract for a simple DApp: Faceblock
/* Can compare deploying a smart contract to tightly versioning an API.
Once a smart contract is distrubted & added to ETH it can never be removed.
Updates to a smart contract, require a re-deployment.
*/
pragma solidity ^ 0.4 .19;
contract FaceBlock {
// We declare high level variables & types used throughout contract
mapping(address => uint256) private addressToIndex;