Skip to content

Instantly share code, notes, and snippets.

@transducer
transducer / custom-resteasy-client.clj
Created November 6, 2023 15:13
Custom RESTEasy client for Keycloak in Clojure
(ns custom-resteasy-client
"Creates a custom `javax.ws.rs.client.Client` with settings for timeouts,
evicting idle connections and a retry handler. This client can be a
resteasyClient for KeycloakBuilder that does not lead to \"RESTEASY004655
Unable to invoke request: java.net.SocketException: Connection reset errors\"
according to the suggestion in
https://github.com/keycloak/keycloak/issues/8917#issuecomment-1712895984."
(:import
(java.util.concurrent TimeUnit)
(org.apache.http.impl.client DefaultHttpRequestRetryHandler HttpClients)
@transducer
transducer / defprogramming
Created November 28, 2018 16:56
defprogramming.org quotes in fortune file
"Formal education will make you a living. Self-education will make you a
fortune."
~ Jim Rohn
%
"Programming is not easy like Sunday morning, it is silent poetry."
~ Waseem Latif
%
"The best way to get the right answer on the internet is not to ask a question;
it's to post the wrong answer."
~ Cunningham's Law
@transducer
transducer / KEY_ROTATION
Last active June 4, 2018 17:02
Example KEY_ROTATION message
{
type: 'KEY_ROTATION,
// IOTA address Side key encrypted with authorized service provider's public key
TOBVJSE9FAAMQGJXYDB...: 'TYBD9HVRWENQKYLKRAKZOHNWJYMDTTVXGOP...',
CUEIYQBOFIYGRBMOEG9...: 'ACNIUSZKCMSSEQ9ILSEACKWLQWSYMRAZYIR...'
}
@transducer
transducer / device-client.js
Created June 4, 2018 16:48
Send encrypted MAM_DATA to a service provider.
/**
* Send encrypted MAM data to a service provider.
*
* @function sendMamData
* @param {string} seed IOTA seed of sender
* @param {string} address IOTA address of the service provider
* @param {string} publicKey Public key of the service provider in trytes
* @returns {Promise} containing IOTA transactions
*/
async sendMamData(seed, address, publicKey) {
@transducer
transducer / device-client.js
Created June 4, 2018 16:41
Process an authorization revoked message
/**
* Process MAM message revoked: authorization
* @param message {Object} MAM message of type {@link AUTHORIZATION_REVOKED_TYPE}.
* @returns {undefined}
*/
processAuthorizationRevokedMessage(message) {
const newSideKey = DeviceClient.createSideKey();
this.authorizedServiceProviders.remove(message.policy.serviceProvider);
this.informUpdateSideKey(
this.authorizedServiceProviders.getAll(),
@transducer
transducer / device-client.js
Created June 4, 2018 16:40
Add policy and inform service provider on address of MAM_DATA
/**
* Process MAM message for added authorization.
* @param message {Object} MAM message of type {@link AUTHORIZED_TYPE}.
* @returns {undefined}
*/
processAuthorizedMessage(message) {
const { serviceProvider } = message.policy;
logger.info(`Authorizing service provider ${JSON.stringify(serviceProvider)}`);
this.authorizedServiceProviders.add(serviceProvider);
this.sendMamData(this.seed, serviceProvider.iotaAddress, serviceProvider.publicKeyTrytes);
@transducer
transducer / device-client.js
Created June 4, 2018 16:39
Process MAM message
/**
* Retrieves and processes MAM messages by dispatching the type of message to
* its handler.
*
* @function processMamMessage
* @returns {undefined}
*/
async processMamMessage() {
const IS_PAIRED = (typeof this.root !== 'undefined');
if (!IS_PAIRED) {
@transducer
transducer / iota-mam.js
Last active June 4, 2018 16:34
Wrapper to attach MAM message to Tangle
/**
* Attach an MAM message.
* @function attach
* @param {JSON} packet JSON packet to attach.
* @returns {Promise} Containing the root or error
*/
async attach(packet) {
this.logger.info(`Attaching packet ${util.inspect(packet)} to the Tangle`);
const trytes = iota.api.toTrytes(JSON.stringify(packet));
@transducer
transducer / p1.js
Created June 4, 2018 16:26
Read P1 port
/**
* Starts serial port reader. Retries via {@link tryInitP1} when port is
* disconnected or on error.
*
* @function tryInitP1
* @param {function} messageHandler What to do with the message
* @returns {undefined}
*/
function initP1(messageHandler) {
logger.info(`Initializing P1 reader on serial port ${RASPBERRY_PI_USB_PORT}`);
@transducer
transducer / devices.js
Last active June 4, 2018 17:44
Create devices aggregate from MAM event stream
/**
* Creates the devices aggregate from the MAM event stream.
* @function toDevices
* @param {array} messages JSON messages from an MAM event stream
* @returns {array} Array of devices (device is object with address and type)
*/
function toDevices(messages) {
const devicesSet = messages.reduce((devices, { type, device }) => {
switch (type) {
case DEVICE_ADDED_TYPE: