Skip to content

Instantly share code, notes, and snippets.

View leegee's full-sized avatar

Lee Goddard leegee

View GitHub Profile
@leegee
leegee / jwt.js
Last active November 16, 2017 09:49
Google Auth with Node.js JWT
const fetch = require('node-fetch');
const FormData = require('form-data');
const JSONWebToken = require('jsonwebtoken');
const getAuthToken = async function (args){
args.duration = args.duration || 60;
args.scope = args.scope instanceof Array? args.scope.join(' ') : args.scope;
let form = new FormData();
form.append('grant_type', 'urn:ietf:params:oauth:grant-type:jwt-bearer');
form.append('scope', args.scope);
@leegee
leegee / eg.pl
Created November 13, 2017 08:51
Verify Google User with Perl
# @see https://developers.google.com/identity/sign-in/web/backend-auth
sub verify_with_google {
my $self = shift;
my $res = LWP::UserAgent->new()->get(
'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token='
. $self->{id_token}
);
if ($res->is_success() and
$res->decoded_content =~ /"aud"\s*:\s*"$self->{client_id}"/
) {
@leegee
leegee / jwt.pl
Last active November 13, 2017 07:03
Google REST API with Perl JSON::WebToken
use strict;
use warnings;
require JSON::XS;
require JSON::WebToken;
require LWP::UserAgent;
my $google_ua = signin_with_google(
service_account_id => $SERVICE_AC_ID,
private_key => $PRIVATE_KEY,
@leegee
leegee / login-with-google.js
Last active October 23, 2019 13:39
Protractor Google OAuth2 Manual Sign-in
loginWithGoogle(
process.env.PROJ_TEST1_GMAIL_USER,
process.env.PROJ_TEST1_GMAIL_PASS
)
/**
* Uses the dreaded `sleep` method because finding the password
* by any css selector tried fails.
* @param {string} username - A Google username.
* @param {string} passphrase - A Google passpharse.
var Wizard = function (state) {
this.state = {};
// pageName: null,
// lastPageName: null
// window.history.popstate();?
Object.keys(state).forEach((key) => {
this.state[key] = state[key];
});
this.pageEl = null;
this.el = {
@leegee
leegee / eg.js
Created November 2, 2017 14:12
Handlebars template to include other templates from DOM
el.main.innerHTML = Handlebars.compile(
el.innerHTML.toString()
)(
{ myContext: this }
);
// {{#includeDomTemplate "publish-tables-and-skus"}}{{/includeDomTemplate}}
Handlebars.registerHelper('includeDomTemplate', function(templateId) {
var html = Handlebars.compile(
document.getElementById(templateId).innerHTML.toString()
@leegee
leegee / tinywizard.js
Last active October 3, 2017 10:41
Tiny Wizard
var Wizard = function (access_token) {
this.access_token = access_token;
this.page = 0;
this.el = {
main: document.getElementById('main'),
pageNumber: document.getElementById('pageNumber')
};
this.el.pageNumber.setAttribute('style', 'display:block"');
}
@leegee
leegee / cache.js
Created November 8, 2016 09:21
Node DNS caching with Promises
var dns = require('dns'), cache = {};
dns._lookup = dns.lookup;
dns.lookup = function(domain) {
return new Promise( (resolve, reject) => {
var key = domain;
if (key in cache) {
var ip = cache[key],
ipv = ip.indexOf('.') !== -1 ? 4 : 6;
@leegee
leegee / base.js
Created June 23, 2016 07:27
log4js base
// base.js
function Base(options) {
var category;
// https://github.com/v8/v8/wiki/Stack-Trace-API
try {
category = ((new Error).stack.split('\n'))[2].match(/^\s+at\sBase\.(\w+)/)[1];
}
catch (e) {
try {
Base.prototype.ERROR = [];
Base.errorNames = [
'PageContext', 'NotYetSupported', 'NaN', 'ColDefPropertyNotFound',
'ColDefValueNotFound', 'InvalidArgument', 'MissingArgument',
'UnknownError', 'InvalidCssSelector'
];
Base.errorNames.forEach(function(errName) {
Base.prototype.ERROR[errName] = function (message) {
Error.captureStackTrace(this, this.constructor);