Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions api_keys/client/api_key.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { ApiKeys } from '/api_keys/collection';
import { TAPi18n } from 'meteor/tap:i18n';
import { sAlert } from 'meteor/juliancwirko:s-alert';
import Clipboard from 'clipboard';

Template.apiKey.onCreated(function () {
// Subscribe to apiKeys for current user
this.subscribe('apiKeysForCurrentUser');
});

Template.apiKey.onRendered(function () {
// Get reference of template instance
instance = this;
const instance = this;

// Initialize Clipboard copy button
instance.copyButton = new Clipboard('#copy-api-key');

// Tell the user when copy is successful
instance.copyButton.on('success', function (event) {
instance.copyButton.on('success', (event) => {
// Get localized success message
const message = TAPi18n.__('apiKeys_copySuccessful');

Expand All @@ -34,27 +37,37 @@ Template.apiKey.onDestroyed(function () {
});

Template.apiKey.events({
'click #get-api-key': function (event) {
'click #get-api-key': function () {
// Get current template instance
const instance = Template.instance();

// Get processing message translation
const message = TAPi18n.__('apiKeys_getApiKeyButton_processing');
// Set bootstrap loadingText
$('#get-api-key').button({ loadingText: message });
instance.$('#get-api-key').button({ loadingText: message });

// Set button to processing state
$('#get-api-key').button('loading');

// Call createApiKey function
Meteor.call('createApiKey', (error, result) => {
if (error) {
sAlert.error(error);
} else {
// Get success message translation
const message = TAPi18n.__('apiKeys_getApiKeyButton_success');

// Alert the user of success
sAlert.success(message);
}
});
instance.$('#get-api-key').button('loading');

// Get api from template data
const api = Template.currentData().api;

// Check api is defined
if (api) {
// Call createApiKey function
Meteor.call('createApiKey', api._id, (error, result) => {
if (error) {
// Show human-readable reason for error
sAlert.error(error.reason);
} else {
// Get success message translation
const successMessage = TAPi18n.__('apiKeys_getApiKeyButton_success');

// Alert the user of success
sAlert.success(successMessage);
}
});
}
},
});

Expand All @@ -69,10 +82,13 @@ Template.apiKey.helpers({
// Get current user
const currentUserId = Meteor.userId();

// Get proxyBackend from template data
const proxyBackend = Template.currentData().proxyBackend;

// Make sure user exists and has API key
if (currentUserId) {
if (proxyBackend && currentUserId) {
// Get API Key document
const userApiKey = ApiKeys.findOne({ userId: currentUserId });
const userApiKey = ApiKeys.findOne({ userId: currentUserId, proxyId: proxyBackend.proxyId });

// Check that Umbrella API key exists
if (userApiKey && userApiKey.apiUmbrella) {
Expand Down
74 changes: 50 additions & 24 deletions api_keys/server/methods.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,66 @@
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/tap:i18n';
import { ApiKeys } from '/api_keys/collection';
import { ProxyBackends } from '/proxy_backends/collection';
import { Proxies } from '/proxies/collection';

Meteor.methods({
createApiKey () {
createApiKey (idApi) {
// Get logged in user
const currentUser = Meteor.user();
// Check currentUser exists
if (currentUser) {
// TODO: Fix for multi-proxy support
const proxy = Proxies.findOne();
const proxyBackend = ProxyBackends.findOne({ apiId: idApi });

// Check type & call appropriate function
if (proxy && proxy.type === 'apiUmbrella') {
// Call Umbrella method to create user with API key
Meteor.call('createApiUmbrellaUser', currentUser, (error, umbrellaUser) => {
if (error) {
console.log(error);
} else {
// Construct apiKey object
const apiKey = {
apiUmbrella: {
id: umbrellaUser.id,
apiKey: umbrellaUser.api_key,
},
userId: currentUser._id,
proxyId: proxy._id,
};
// Check proxyBackend is defined, and it has proxyId
if (proxyBackend && proxyBackend.proxyId) {
// Get Proxy by proxyId of proxyBackend
const proxyId = proxyBackend.proxyId;
const proxy = Proxies.findOne({ _id: proxyId });

// Insert apiKey
ApiKeys.insert(apiKey);
}
});
// Check type & call appropriate function
if (proxy && proxy.type === 'apiUmbrella') {
// Call Umbrella method to create user with API key
Meteor.call('createApiUmbrellaUser', currentUser, proxyId, (error, umbrellaUser) => {
if (error) {
// Log error for server
console.log(error);
// Throw apiumbrellauser error for client
throw new Meteor.Error(
'apinf-apiumbrellauser-error',
TAPi18n.__('apinf_apiumbrellauser_error')
);
} else {
// Construct apiKey object
const apiKey = {
apiUmbrella: {
id: umbrellaUser.id,
apiKey: umbrellaUser.api_key,
},
userId: currentUser._id,
proxyId: proxy._id,
};

// Insert apiKey
ApiKeys.insert(apiKey);
}
});
} else {
// Throw no proxy error for client
throw new Meteor.Error(
'apinf-noproxy-error',
TAPi18n.__('apinf_noproxy_error')
);
}
} else {
// Throw no proxybackend error for client
throw new Meteor.Error(
'apinf-noproxybackend-error',
TAPi18n.__('apinf_noproxybackend_error')
);
}
} else {
// Meteor Error (User not logged in)
// Throw usernotloggedin error for client
throw new Meteor.Error(
'apinf-usernotloggedin-error',
TAPi18n.__('apinf_usernotloggedin_error')
Expand Down
2 changes: 1 addition & 1 deletion apis/client/view/header/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h1 id="api-header">
{{#if proxyBackend }}
<div>
<span class="pull-right">
{{> apiKey }}
{{> apiKey api=api proxyBackend=proxyBackend }}
</span>
</div>
{{/if }}
Expand Down
5 changes: 4 additions & 1 deletion core/lib/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@
"apiSettings_visibility_text": "Make this API private or public",
"apiSettings_visibility_authorizedUsers": "Give single users permissions to view API even when API is globally marked Private.",
"api_backend_rating_anonymous": "Please log in to vote.",
"apinf_usernotloggedin_error": "Could not find signed-in user.",
"apinf_apiumbrellauser_error": "Error: Could not create user on proxy",
"apinf_noproxy_error": "Error: No proxy found",
"apinf_noproxybackend_error": "Error: No proxyBackend found",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something like "No proxy backend found."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing it tomorrow :)

"apinf_usernotloggedin_error": "Error: Could not find signed-in user.",
"apiMonitoring_panelTitle_Monitoring": "API Monitoring",
"apiMonitoring_helpIcon_text": "Please provide a valid path to an endpoint to make actual API calls. API monitoring is done using HTTP requests. You can use GET method only so that accidental data insertion in your API is prevented.",
"apiMonitoring_saveButton_text": "Save",
Expand Down
2 changes: 1 addition & 1 deletion core/lib/i18n/fi.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
"apiSettings_visibility_heading": "Muuta rajapinnan n\u00e4kyvyytt\u00e4",
"apiSettings_visibility_text": "Muuta rajapinta julkiseksi tai yksityiseksi",
"api_backend_rating_anonymous": "Kirjaudu sis\u00e4\u00e4n pisteytt\u00e4\u00e4ksesi rajapinta.",
"apinf_usernotloggedin_error": "Kirjautunutta k\u00e4ytt\u00e4j\u00e4\u00e4 ei l\u00f6ytynyt.",
"apinf_usernotloggedin_error": "Virhe: Kirjautunutta k\u00e4ytt\u00e4j\u00e4\u00e4 ei l\u00f6ytynyt.",
"apiProxy_headerText": "Rajapinnan v\u00e4lityspalvelinasetukset",
"backlogItem_addedByYou": "Lis\u00e4nnyt: nykyinen k\u00e4ytt\u00e4j\u00e4",
"backlogItem_editButton_text": "Muokkaa",
Expand Down