Skip to content

Instantly share code, notes, and snippets.

@philpalmieri
Created July 29, 2017 18:35
Show Gist options
  • Save philpalmieri/72938bcd2c58f2c5a44b46e40cf02fc7 to your computer and use it in GitHub Desktop.
Save philpalmieri/72938bcd2c58f2c5a44b46e40cf02fc7 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import {environment} from "../../environments/environment";
/** Encryption Stuff **/
var CryptoJS = require("crypto-js");
var hmacsha1 = require('hmacsha1');
@Injectable()
export class ContentService{
private baseUrl: string = '';
private formUrl: string = '';
constructor(private http:Http) {
this.baseUrl = environment.basePath;
this.formUrl = environment.formPath;
}
public get(url, fullPath = false) {
let fullURL: string = '';
if(fullPath) {
fullURL = url;
} else {
fullURL = this.baseUrl + url;
}
return this.http.get(fullURL).map((resp) => resp.json());
}
public gformPost(url:string, data) {
let fullURL: string = this.formUrl + url;
let d = new Date;
let expiration = 3600; // 1 hour,
let unixtime = d.getTime() / 1000 | 0;
let expires = unixtime + expiration;
let stringToSign = environment.gformApiKey + ":POST:" + url + ":" + expires;
let signature = this.gformSignature(stringToSign);
let completeUrl = fullURL+"?api_key="+environment.gformApiKey+"&signature="+signature+"&expires="+expires;
return this.http.post(completeUrl, JSON.stringify(data))
.map(res => res.json());
}
private gformSignature(toencrypt:string) {
let hash = CryptoJS.HmacSHA1(toencrypt, environment.gformPrivateKey);
let base64 = hash.toString(CryptoJS.enc.Base64);
return encodeURIComponent(base64);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment