Skip to content
Snippets Groups Projects
Commit 871cb357 authored by Gospodin Bodurov's avatar Gospodin Bodurov
Browse files

Merge branch 'feature-vcl-webpack-bundle' into 'master'

Use webpack for bundling JS libs and move Golang code related to Iframe generation there

See merge request !4
parents f0328c6f c25c5a33
Branches
Tags
1 merge request!4Use webpack for bundling JS libs and move Golang code related to Iframe generation there
Showing with 8210 additions and 102 deletions
.idea
*.iml
bin/
Gopkg.lock
vendor/
temp/
yarn-error.log
\ No newline at end of file
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
branch = "feature-move-js-libs-to-vcl"
name = "code.vereign.com/code/restful-api"
[prune]
go-tests = true
unused-packages = true
Makefile 0 → 100644
SERVER_OUT := "bin/app"
PKG := "code.vereign.com/code/vcl"
SERVER_PKG_BUILD := "${PKG}"
IFRAME_LIB_LOCATION := "javascript/temp"
.PHONY: all build run clean
all: build run webpack-build clean
dev: build run webpack-dev
dep: ## Get the dependencies
dep ensure
build: dep ## Build the binary file for app
@go build -i -v -o $(SERVER_OUT) $(SERVER_PKG_BUILD)
run: build ## Run app to build iframe lib containing Restful API endpoints
./bin/app
webpack-build: run ## Builds JS bundles
cd javascript && yarn install && yarn build
webpack-dev: run ## Builds JS bundles
cd javascript && yarn install && yarn start
clean: webpack-build ## Remove previous builds
@rm $(SERVER_OUT)
@rm -rf $(IFRAME_LIB_LOCATION)
help: ## Display this help screen
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
fresh:
rm -rf ./vendor
rm -rf ./Gopkg.lock
\ No newline at end of file
# Vereign Client Library (VCL)
Vereign’s JavaScript library is an asynchronous library that allows for digitally signing your email and in-browser documents, data encryption, and key-based authentication.
## Usage
Add library as inline script.
```html
<script type="text/javascript" src="./viamapi-client.js"/>
```
Initialize library
```js
// Target container used to append iframe
const containerId = "root";
// Required methods used to be called within iframe.
const methods = {
onEvent(event) { console.log(event) }
};
// Iframe URL to connect
const iframeUrl = "https://test.org/iframe";
// API url used to access endpoints
const apiUrl = "https://test.org/api";
this.library = setupViamAPI(containerId, methods, iframeUrl, apiUrl);
this.library.then(lib => {
this.listenerUUID = this.addListener({
callback: function(event) {
handleEvent(event);
},
filters: [
"IdentityNotLoaded",
"ActionConfirmedAndExecuted",
"Authenticated"
]
});
lib.listIdentities().then(response => {
this.setState({ identities: response.data });
});
});
```
## Development
### Running
Run `make dev` to enter development mode. Library will be built with all Restful API endpoints and served from `localhost:9000`. If you edit JS libs code, it will be reloaded on the fly.
- Client being served from `http://localhost:9000/viamapi-client.js`
- Iframe being served from `http://localhost:9000/viamapi-iframe.html`
You can specify these URLs in the clients.
If you have to do changes in `main.go`, you need interrupt current process and do `make dev` again.
### CORS
Since libs and restful-api being served within different ports during the development,
it worth mention that responses from restful-api in dev mode must include `"Access-Control-Allow-Origin": "http://localhost:9000"`
and `"Access-Control-Allow-Headers": "*"` headers.
## Building
Run `make` and get client library and iframe bundles from `javascript/dist`
node_modules
dist
......@@ -2,17 +2,15 @@
"name": "vereign-client-lib",
"description": "Vereign 3rd Party Integration Client Library",
"version": "1.0.0",
"scripts": {
"start": "webpack-dev-server --config webpack.config.dev.js",
"build": "webpack --config webpack.config.prod.js"
},
"repository": {
"type": "git",
"url": "git@code.vereign.com:code/vcl.git"
},
"homepage": "https://code.vereign.com/code/vcl",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {},
"devDependencies": {},
"keywords": [
"vereign",
"library",
......@@ -24,6 +22,28 @@
"url": "https://www.vereign.com/"
},
"license": "MIT",
"testling": {},
"scripts": {}
"dependencies": {
"asn1js": "^2.0.21",
"axios": "0.18.0",
"libmime": "^4.0.1",
"penpal": "^3.0.3",
"pkijs": "^2.1.69",
"pvutils": "^1.0.16",
"qrcode": "^1.3.0"
},
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"babel-loader": "^8.0.4",
"babel-polyfill": "^6.26.0",
"clean-webpack-plugin": "^0.1.19",
"core-js": "^2.5.7",
"html-webpack-inline-source-plugin": "^0.0.10",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.23.1",
"webpack-clean": "^1.2.3",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10",
"webpack-merge": "^4.1.4"
}
}
This diff is collapsed.
const axios = require('axios')
function WopiAPI() {}
WopiAPI.prototype.getPassports = function(publicKey, uuid, token, fileID) {
const requestConfig = {
headers: {
'publicKey': publicKey,
'uuid': uuid,
'token': token,
'fileID': fileID
}
};
const urlParts = window.API_HOST.split(':');
return axios.post(urlParts[0] + urlParts[1] + ':8787/getPassports', {}, requestConfig);
};
module.exports = WopiAPI;
\ No newline at end of file
const Penpal = require('penpal').default;
/**
* Sets up interaction with Vereign Restful API
* @param divId - target container to append iframe via Penpal
* @param methods - list of methods to be used in iframe
* @param iframeUrl - iframe URL to connect
* @param apiUrl - API URL used to access API endpoints
* @returns {*}
*/
function setupViamAPI(divId, methods, iframeUrl, apiUrl) {
if (!apiUrl) {
return Promise.reject('API Host not specified.')
}
const connection = Penpal.connectToChild({
// URL of page to load into iframe.
url: iframeUrl,
// Container to which the iframe should be appended.
appendTo: document.getElementById(divId),
// Methods parent is exposing to child
methods
});
return connection.promise
.then((child) => child.initializeApiHost(apiUrl).then(() => child))
}
window.setupViamAPI = setupViamAPI;
const merge = require('webpack-merge');
const webpack = require('webpack');
const common = require('./webpack.config');
module.exports = merge(common, {
devtool: 'source-map',
mode: 'development',
devServer: {
contentBase: './dist',
// hot: true,
port: 9000,
headers: {
'Access-Control-Allow-Origin': '*'
}
},
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
});
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin')
module.exports = {
entry: {
'viamapi-client': ['core-js/fn/promise', './src/viamapi-client.js'],
'viamapi-iframe': ['babel-polyfill', './src/iframe/viamapi-iframe.js'],
},
plugins: [
new HtmlWebpackPlugin({ // Also generate a test.html
filename: 'viamapi-iframe.html',
template: 'src/iframe/viamapi-iframe.html',
chunks: ['viamapi-iframe'],
inlineSource: '.(js)$'
}),
new CleanWebpackPlugin(),
new HtmlWebpackInlineSourcePlugin()
],
module: {
rules: [
{
test: /\.m?js$/,
// We have to transpile every dependency to make it work for older browsers. (IE 11)
//exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
};
const merge = require('webpack-merge');
const WebpackCleanPlugin = require('webpack-clean');
const common = require('./webpack.config');
module.exports = merge(common, {
mode: 'production',
plugins: [
new WebpackCleanPlugin('dist/viamapi-iframe.js')
]
});
\ No newline at end of file
This diff is collapsed.
main.go 0 → 100644
package main
import (
"bytes"
"code.vereign.com/code/restful-api/server"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"unicode"
"unicode/utf8"
)
func main() {
newpath := filepath.Join(".", "javascript/temp")
os.MkdirAll(newpath, os.ModePerm)
viamapi := buildViamApi()
err := ioutil.WriteFile("./javascript/temp/viamapi.js", []byte(viamapi), 0644)
if err != nil {
fmt.Println(err.Error())
}
penpalMethods := buildPenpalMethods()
err = ioutil.WriteFile("./javascript/temp/penpal-methods.js", []byte(penpalMethods), 0644)
if err != nil {
fmt.Println(err.Error())
}
}
func buildPenpalMethods() string {
prefixes := []string{}
endPoints := server.GetEndPoints(prefixes)
result :=
"import Penpal from 'penpal';\n\n" +
"export default {\n"
methods := generatePenpalRemoteMethods(endPoints)
methods += getWopiAPIPenpalMethods()
result += methods
result += "\n}"
return result
}
func buildViamApi() string {
prefixes := []string{}
endPoints := server.GetEndPoints(prefixes)
result := "const axios = require('axios');\n\n"
var keys []string
for k := range endPoints {
keys = append(keys, k)
}
sort.Strings(keys)
keysLen := len(keys)
result += "function ViamAPI() {\n" +
" this.config = {\n" +
" headers: {\n" +
" 'publicKey': '',\n" +
" 'uuid': '',\n" +
" 'token': ''\n" +
" }\n" +
" }\n" +
"}\n\n"
result += "ViamAPI.prototype.setSessionData = function(uuid, token) {\n" +
" this.config.headers.uuid = \" \" + uuid;\n" +
" this.config.headers.token = \" \" + token;\n" +
"};\n\n"
result += "ViamAPI.prototype.setIdentity = function(authenticationPublicKey) {\n" +
" this.config.headers.publicKey = \" \" + window.btoa(authenticationPublicKey);\n" +
"};\n\n"
result += "ViamAPI.prototype.getConfig = function() {\n" +
" return this.config;\n" +
"};\n\n"
for i := 0; i < keysLen; i++ {
url := keys[i]
if endPoints[url].Form != nil {
splits := strings.Split(url, "/")
packageStr := splits[len(splits)-2]
/*if !packageCreated[packageStr] {
result += "ViamAPI.prototype.\"" + packageStr + "\" = {}\n\n"
packageCreated[packageStr] = true
}*/
methodStr := splits[len(splits)-1]
form := endPoints[url].Form
s := reflect.ValueOf(form)
typeOfT := s.Type()
fields := typeFields(typeOfT)
lenFields := len(fields)
args := ""
for i := 0; i < lenFields; i++ {
if i != lenFields-1 {
args += fmt.Sprintf("%sArg,", fields[i].name)
} else {
args += fmt.Sprintf("%sArg", fields[i].name)
}
}
result += "ViamAPI.prototype." + packageStr + strings.Title(methodStr) + " = function(" + args + ") {\n" +
" return axios.post(window.API_HOST + '" + packageStr + "/" + methodStr + "', {\n"
for i := 0; i < lenFields; i++ {
result += " " + fields[i].name + ": " + fields[i].name + "Arg"
if i != lenFields-1 {
result += ","
}
result += "\n"
}
result += " }, this.config);\n" +
"};\n\n"
} else {
splits := strings.Split(url, "/")
packageStr := splits[len(splits)-2]
/*if !packageCreated[packageStr] {
result += "api[\"" + packageStr + "\"] = {}\n"
packageCreated[packageStr] = true
}*/
methodStr := splits[len(splits)-1]
result += "ViamAPI.prototype." + packageStr + strings.Title(methodStr) + " = function() {\n" +
" return axios.post(window.API_HOST + '" + packageStr + "/" + methodStr + "', {}, this.config);\n" +
"};\n\n"
}
}
result += "module.exports = ViamAPI;\n"
return result
}
const (
caseMask = ^byte(0x20) // Mask to ignore case in ASCII.
kelvin = '\u212a'
smallLongEss = '\u017f'
)
// equalFoldRight is a specialization of bytes.EqualFold when s is
// known to be all ASCII (including punctuation), but contains an 's',
// 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t.
// See comments on foldFunc.
func equalFoldRight(s, t []byte) bool {
for _, sb := range s {
if len(t) == 0 {
return false
}
tb := t[0]
if tb < utf8.RuneSelf {
if sb != tb {
sbUpper := sb & caseMask
if 'A' <= sbUpper && sbUpper <= 'Z' {
if sbUpper != tb&caseMask {
return false
}
} else {
return false
}
}
t = t[1:]
continue
}
// sb is ASCII and t is not. t must be either kelvin
// sign or long s; sb must be s, S, k, or K.
tr, size := utf8.DecodeRune(t)
switch sb {
case 's', 'S':
if tr != smallLongEss {
return false
}
case 'k', 'K':
if tr != kelvin {
return false
}
default:
return false
}
t = t[size:]
}
if len(t) > 0 {
return false
}
return true
}
// asciiEqualFold is a specialization of bytes.EqualFold for use when
// s is all ASCII (but may contain non-letters) and contains no
// special-folding letters.
// See comments on foldFunc.
func asciiEqualFold(s, t []byte) bool {
if len(s) != len(t) {
return false
}
for i, sb := range s {
tb := t[i]
if sb == tb {
continue
}
if ('a' <= sb && sb <= 'z') || ('A' <= sb && sb <= 'Z') {
if sb&caseMask != tb&caseMask {
return false
}
} else {
return false
}
}
return true
}
// simpleLetterEqualFold is a specialization of bytes.EqualFold for
// use when s is all ASCII letters (no underscores, etc) and also
// doesn't contain 'k', 'K', 's', or 'S'.
// See comments on foldFunc.
func simpleLetterEqualFold(s, t []byte) bool {
if len(s) != len(t) {
return false
}
for i, b := range s {
if b&caseMask != t[i]&caseMask {
return false
}
}
return true
}
func foldFunc(s []byte) func(s, t []byte) bool {
nonLetter := false
special := false // special letter
for _, b := range s {
if b >= utf8.RuneSelf {
return bytes.EqualFold
}
upper := b & caseMask
if upper < 'A' || upper > 'Z' {
nonLetter = true
} else if upper == 'K' || upper == 'S' {
// See above for why these letters are special.
special = true
}
}
if special {
return equalFoldRight
}
if nonLetter {
return asciiEqualFold
}
return simpleLetterEqualFold
}
// A field represents a single field found in a struct.
type field struct {
name string
nameBytes []byte // []byte(name)
equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
tag bool
index []int
typ reflect.Type
omitEmpty bool
quoted bool
}
func fillField(f field) field {
f.nameBytes = []byte(f.name)
f.equalFold = foldFunc(f.nameBytes)
return f
}
// byIndex sorts field by index sequence.
type byIndex []field
func (x byIndex) Len() int { return len(x) }
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byIndex) Less(i, j int) bool {
for k, xik := range x[i].index {
if k >= len(x[j].index) {
return false
}
if xik != x[j].index[k] {
return xik < x[j].index[k]
}
}
return len(x[i].index) < len(x[j].index)
}
func isValidTag(s string) bool {
if s == "" {
return false
}
for _, c := range s {
switch {
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
// Backslash and quote chars are reserved, but
// otherwise any punctuation chars are allowed
// in a tag name.
default:
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
}
}
}
return true
}
// tagOptions is the string following a comma in a struct field's "json"
// tag, or the empty string. It does not include the leading comma.
type tagOptions string
// Contains reports whether a comma-separated list of options
// contains a particular substr flag. substr must be surrounded by a
// string boundary or commas.
func (o tagOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, ",")
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}
// parseTag splits a struct field's json tag into its name and
// comma-separated options.
func parseTag(tag string) (string, tagOptions) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tagOptions(tag[idx+1:])
}
return tag, tagOptions("")
}
// typeFields returns a list of fields that JSON should recognize for the given type.
// The algorithm is breadth-first search over the set of structs to include - the top struct
// and then any reachable anonymous structs.
func typeFields(t reflect.Type) []field {
// Anonymous fields to explore at the current level and the next.
current := []field{}
next := []field{{typ: t}}
// Count of queued names for current level and the next.
count := map[reflect.Type]int{}
nextCount := map[reflect.Type]int{}
// Types already visited at an earlier level.
visited := map[reflect.Type]bool{}
// Fields found.
var fields []field
for len(next) > 0 {
current, next = next, current[:0]
count, nextCount = nextCount, map[reflect.Type]int{}
for _, f := range current {
if visited[f.typ] {
continue
}
visited[f.typ] = true
// Scan f.typ for fields to include.
for i := 0; i < f.typ.NumField(); i++ {
sf := f.typ.Field(i)
isUnexported := sf.PkgPath != ""
if sf.Anonymous {
t := sf.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if isUnexported && t.Kind() != reflect.Struct {
// Ignore embedded fields of unexported non-struct types.
continue
}
// Do not ignore embedded fields of unexported struct types
// since they may have exported fields.
} else if isUnexported {
// Ignore unexported non-embedded fields.
continue
}
tag := sf.Tag.Get("json")
if tag == "-" {
continue
}
name, opts := parseTag(tag)
if !isValidTag(name) {
name = ""
}
index := make([]int, len(f.index)+1)
copy(index, f.index)
index[len(f.index)] = i
ft := sf.Type
if ft.Name() == "" && ft.Kind() == reflect.Ptr {
// Follow pointer.
ft = ft.Elem()
}
// Only strings, floats, integers, and booleans can be quoted.
quoted := false
if opts.Contains("string") {
switch ft.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64,
reflect.String:
quoted = true
}
}
// Record found field and index sequence.
if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
tagged := name != ""
if name == "" {
name = sf.Name
}
fields = append(fields, fillField(field{
name: name,
tag: tagged,
index: index,
typ: ft,
omitEmpty: opts.Contains("omitempty"),
quoted: quoted,
}))
if count[f.typ] > 1 {
// If there were multiple instances, add a second,
// so that the annihilation code will see a duplicate.
// It only cares about the distinction between 1 or 2,
// so don't bother generating any more copies.
fields = append(fields, fields[len(fields)-1])
}
continue
}
// Record new anonymous struct to explore in next round.
nextCount[ft]++
if nextCount[ft] == 1 {
next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft}))
}
}
}
}
sort.Slice(fields, func(i, j int) bool {
x := fields
// sort field by name, breaking ties with depth, then
// breaking ties with "name came from json tag", then
// breaking ties with index sequence.
if x[i].name != x[j].name {
return x[i].name < x[j].name
}
if len(x[i].index) != len(x[j].index) {
return len(x[i].index) < len(x[j].index)
}
if x[i].tag != x[j].tag {
return x[i].tag
}
return byIndex(x).Less(i, j)
})
// Delete all fields that are hidden by the Go rules for embedded fields,
// except that fields with JSON tags are promoted.
// The fields are sorted in primary order of name, secondary order
// of field index length. Loop over names; for each name, delete
// hidden fields by choosing the one dominant field that survives.
out := fields[:0]
for advance, i := 0, 0; i < len(fields); i += advance {
// One iteration per name.
// Find the sequence of fields with the name of this first field.
fi := fields[i]
name := fi.name
for advance = 1; i+advance < len(fields); advance++ {
fj := fields[i+advance]
if fj.name != name {
break
}
}
if advance == 1 { // Only one field with this name
out = append(out, fi)
continue
}
dominant, ok := dominantField(fields[i : i+advance])
if ok {
out = append(out, dominant)
}
}
fields = out
sort.Sort(byIndex(fields))
return fields
}
// dominantField looks through the fields, all of which are known to
// have the same name, to find the single field that dominates the
// others using Go's embedding rules, modified by the presence of
// JSON tags. If there are multiple top-level fields, the boolean
// will be false: This condition is an error in Go and we skip all
// the fields.
func dominantField(fields []field) (field, bool) {
// The fields are sorted in increasing index-length order. The winner
// must therefore be one with the shortest index length. Drop all
// longer entries, which is easy: just truncate the slice.
length := len(fields[0].index)
tagged := -1 // Index of first tagged field.
for i, f := range fields {
if len(f.index) > length {
fields = fields[:i]
break
}
if f.tag {
if tagged >= 0 {
// Multiple tagged fields at the same level: conflict.
// Return no field.
return field{}, false
}
tagged = i
}
}
if tagged >= 0 {
return fields[tagged], true
}
// All remaining fields have the same length. If there's more than one,
// we have a conflict (two fields named "X" at the same level) and we
// return no field.
if len(fields) > 1 {
return field{}, false
}
return fields[0], true
}
func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string {
var keys []string
for k := range endPoints {
keys = append(keys, k)
}
sort.Strings(keys)
keysLen := len(keys)
methods := ""
privateCheckSnippet := `
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (authenticationPublicKey === null) {
result({
"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
if (loadedIdentities[authenticationPublicKey] === null) {
result({
"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
const success = extendPinCodeTtl(authenticationPublicKey);
if(success === false) {
result({"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
})
}
`
for i := 0; i < keysLen; i++ {
url := keys[i]
if endPoints[url].ManuallyWritten == true {
continue
}
if url == "/identity/getIdentityProfileData" {
privateCheckSnippet = `
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (authenticationPublicKey === null) {
result({
"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
if (loadedIdentities[authenticationPublicKey] === null) {
result({
"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
`
}
if endPoints[url].Form != nil {
splits := strings.Split(url, "/")
packageStr := splits[len(splits)-2]
/*if !packageCreated[packageStr] {
result += "ViamAPI.prototype.\"" + packageStr + "\" = {}\n\n"
packageCreated[packageStr] = true
}*/
methodStr := splits[len(splits)-1]
form := endPoints[url].Form
s := reflect.ValueOf(form)
typeOfT := s.Type()
fields := typeFields(typeOfT)
lenFields := len(fields)
args := ""
for i := 0; i < lenFields; i++ {
if i != lenFields-1 {
args += fmt.Sprintf("%sArg,", fields[i].name)
} else {
args += fmt.Sprintf("%sArg", fields[i].name)
}
}
/*identity_login(modeArg,codeArg,actionIDArg) {
return new Penpal.Promise(result => {
viamApi.identity_login(modeArg,codeArg,actionIDArg).then((response) => { result(response.data);});
});
}*/
snippet := ""
if endPoints[url].HandlerType == "private" {
snippet = privateCheckSnippet
}
lastComma := ","
if args == "" {
lastComma = ""
}
method := packageStr + strings.Title(methodStr) + ": function(" + args + ") {\n" +
" return new Penpal.Promise(function(result) {\n" + snippet +
" executeRestfulFunction(\"" + endPoints[url].HandlerType + "\", viamApi, viamApi." + packageStr + strings.Title(methodStr) + lastComma + args + ").then(function(executeResult) {\n" +
" result(executeResult);\n" +
" });\n" +
" });\n" +
"}"
methods += method
if i != keysLen-1 {
methods += ","
}
methods += "\n"
} else {
splits := strings.Split(url, "/")
packageStr := splits[len(splits)-2]
/*if !packageCreated[packageStr] {
result += "ViamAPI.prototype.\"" + packageStr + "\" = {}\n\n"
packageCreated[packageStr] = true
}*/
methodStr := splits[len(splits)-1]
snippet := ""
if endPoints[url].HandlerType == "private" {
snippet = privateCheckSnippet
}
method := packageStr + strings.Title(methodStr) + ": function() {\n" +
" return new Penpal.Promise(function(result) {\n" + snippet +
" executeRestfulFunction(\"" + endPoints[url].HandlerType + "\", viamApi, viamApi." + packageStr + strings.Title(methodStr) + ").then(function(executeResult) {\n" +
" result(executeResult);\n" +
" });\n" +
" });\n" +
"}"
methods += method
if i != keysLen-1 {
methods += ","
}
methods += "\n"
}
}
return methods
}
func getWopiAPIPenpalMethods() string {
return `getPassports: function(fileID) {
return new Penpal.Promise(function(result) {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (authenticationPublicKey === null) {
result({
"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
if (loadedIdentities[authenticationPublicKey] === null) {
result({
"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
const success = extendPinCodeTtl(authenticationPublicKey);
if(success === false) {
result({"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
wopiAPI.getPassports(
authenticationPublicKey,
viamApi.getConfig().headers.uuid,
viamApi.getConfig().headers.token,
fileID
).then(function(response) {
result(response.data);
});
});
}`
}
# Vereign Client Library (VCL)
Vereign’s JavaScript library is an asynchronous library that allows for digitally signing your email and in-browser documents, data encryption, and key-based authentication.
## Usage
```js
import { setupViamAPI } from "vcl";
// setupViamAPI(divId, methodsArg, envUrl)
function setupViamAPI(divId, methodsArg, envUrl) {
const connection = Penpal.connectToChild({
// URL of page to load into iframe.
url: envUrl,
// Container to which the iframe should be appended.
appendTo: document.getElementById(divId),
// Methods parent is exposing to child
methods: methodsArg
});
return connection.promise
}
```
```js
// Example
this.library = setupViamAPI(
"root",
{
onEvent(event) {
sendEvent(event);
}
},
this.props.configuration.api.endpoint
);
this.library.then(lib => {
this.listenerUUID = this.addListener({
callback: function(event) {
handleEvent(event);
},
filters: [
"IdentityNotLoaded",
"ActionConfirmedAndExecuted",
"Authenticated"
]
});
lib.listIdentities().then(response => {
this.setState({ identities: response.data });
});
});
}
```
## Installation
With [npm](https://npmjs.org) do
```bash
$ npm install vcl
```
## License
(MIT)
Copyright (c) 2018 Vereign AG
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
!function(e,n){var r={};!function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=e.ERR_CONNECTION_DESTROYED="ConnectionDestroyed",r=e.ERR_CONNECTION_TIMEOUT="ConnectionTimeout",t=e.ERR_NOT_IN_IFRAME="NotInIframe",o={"http:":"80","https:":"443"},a=/^(https?:)?\/\/([^\/:]+)(:(\d+))?/,i={ERR_CONNECTION_DESTROYED:n,ERR_CONNECTION_TIMEOUT:r,ERR_NOT_IN_IFRAME:t,Promise:function(){try{return window?window.Promise:null}catch(e){return null}}(),debug:!1},d=function(){var e=0;return function(){return++e}}(),c=function(){for(var e=arguments.length,n=Array(e),r=0;r<e;r++)n[r]=arguments[r];if(i.debug){var t;(t=console).log.apply(t,["[Penpal]"].concat(n))}},u=function(e){var n=document.location,r=a.exec(e),t=void 0,i=void 0,d=void 0;return r?(t=r[1]?r[1]:n.protocol,i=r[2],d=r[4]):(t=n.protocol,i=n.hostname,d=n.port),t+"//"+i+(d&&d!==o[t]?":"+d:"")},s=function(e){var n=[];return e(function(){n.forEach(function(e){e()})}),{then:function(e){n.push(e)}}},l=function(e){return{name:e.name,message:e.message,stack:e.stack}},m=function(e){var n=new Error;return Object.keys(e).forEach(function(r){return n[r]=e[r]}),n},f=function(e,r,t,o){var a=r.localName,u=r.local,s=r.remote,l=r.remoteOrigin,f=!1;c(a+": Connecting call sender");var v=function(e){return function(){for(var r=arguments.length,t=Array(r),o=0;o<r;o++)t[o]=arguments[o];if(c(a+": Sending "+e+"() call"),f){var v=new Error("Unable to send "+e+"() call due to destroyed connection");throw v.code=n,v}return new i.Promise(function(n,r){var o=d(),i=function t(i){if(i.source===s&&i.origin===l&&"reply"===i.data.penpal&&i.data.id===o){c(a+": Received "+e+"() reply"),u.removeEventListener("message",t);var d=i.data.returnValue;i.data.returnValueIsError&&(d=m(d)),("fulfilled"===i.data.resolution?n:r)(d)}};u.addEventListener("message",i),s.postMessage({penpal:"call",id:o,methodName:e,args:t},l)})}};o.then(function(){f=!0}),t.reduce(function(e,n){return e[n]=v(n),e},e)},v=function(e,n,r){var t=e.localName,o=e.local,a=e.remote,d=e.remoteOrigin,u=!1;c(t+": Connecting call receiver");var s=function(e){if(e.source===a&&e.origin===d&&"call"===e.data.penpal){var r=e.data,o=r.methodName,s=r.args,m=r.id;if(c(t+": Received "+o+"() call"),o in n){var f=function(e){return function(n){if(c(t+": Sending "+o+"() reply"),u)return void c(t+": Unable to send "+o+"() reply due to destroyed connection");var r={penpal:"reply",id:m,resolution:e,returnValue:n};"rejected"===e&&n instanceof Error&&(r.returnValue=l(n),r.returnValueIsError=!0);try{a.postMessage(r,d)}catch(e){throw"DataCloneError"===e.name&&a.postMessage({penpal:"reply",id:m,resolution:"rejected",returnValue:l(e),returnValueIsError:!0},d),e}}};new i.Promise(function(e){return e(n[o].apply(n,s))}).then(f("fulfilled"),f("rejected"))}}};o.addEventListener("message",s),r.then(function(){u=!0,o.removeEventListener("message",s)})};i.connectToChild=function(e){var t=e.url,o=e.appendTo,a=e.methods,d=void 0===a?{}:a,l=e.timeout,m=void 0,p=new s(function(e){m=e}),h=window,g=document.createElement("iframe");(o||document.body).appendChild(g),p.then(function(){g.parentNode&&g.parentNode.removeChild(g)});var E=g.contentWindow||g.contentDocument.parentWindow,w=u(t);return{promise:new i.Promise(function(e,o){var a=void 0;void 0!==l&&(a=setTimeout(function(){var e=new Error("Connection to child timed out after "+l+"ms");e.code=r,o(e),m()},l));var i={},u=void 0,y=void 0,N=function(n){if(n.source===E&&n.origin===w&&"handshake"===n.data.penpal){c("Parent: Received handshake, sending reply"),n.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(d)},n.origin);var r={localName:"Parent",local:h,remote:E,remoteOrigin:n.origin};y&&y();var t=new s(function(e){p.then(e),y=e});v(r,d,t),u&&u.forEach(function(e){delete i[e]}),u=n.data.methodNames,f(i,r,u,p),clearTimeout(a),e(i)}};h.addEventListener("message",N),p.then(function(){h.removeEventListener("message",N);var e=new Error("Connection destroyed");e.code=n,o(e)}),c("Parent: Loading iframe"),g.src=t}),iframe:g,destroy:m}},i.connectToParent=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},o=e.parentOrigin,a=void 0===o?"*":o,d=e.methods,u=void 0===d?{}:d,l=e.timeout;if(window===window.top){var m=new Error("connectToParent() must be called within an iframe");throw m.code=t,m}var p=void 0,h=new s(function(e){p=e}),g=window,E=g.parent;return{promise:new i.Promise(function(e,t){var o=void 0;void 0!==l&&(o=setTimeout(function(){var e=new Error("Connection to parent timed out after "+l+"ms");e.code=r,t(e),p()},l));var i=function n(r){if(("*"===a||a===r.origin)&&r.source===E&&"handshake-reply"===r.data.penpal){c("Child: Received handshake reply"),g.removeEventListener("message",n);var t={localName:"Child",local:g,remote:E,remoteOrigin:r.origin},i={};v(t,u,h),f(i,t,r.data.methodNames,h),clearTimeout(o),e(i)}};g.addEventListener("message",i),h.then(function(){g.removeEventListener("message",i);var e=new Error("Connection destroyed");e.code=n,t(e)}),c("Child: Sending handshake"),E.postMessage({penpal:"handshake",methodNames:Object.keys(u)},a)}),destroy:p}},e.default=i}(r),"function"==typeof define&&define.amd?define("Penpal",r.default):e.Penpal=r.default}(this);
function setupViamAPI(divId, methodsArg, envUrl) {
const connection = Penpal.connectToChild({
// URL of page to load into iframe.
url: envUrl,
// Container to which the iframe should be appended.
appendTo: document.getElementById(divId),
// Methods parent is exposing to child
methods: methodsArg
});
return connection.promise
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment