Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

71
LINES

< > BotCompany Repo | #1030463 // Web Push Manager

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 15986K of libraries. Click here for Pure Java version (12507L/65K).

!7

!include once #1030462 // BouncyCastle

import nl.martijndwars.webpush.Subscription;
import nl.martijndwars.webpush.PushService;
import nl.martijndwars.webpush.Notification;

concept Entry {
  S publicKey;
  SecretValue<S> privateKey;
  S comment;
  S globalID = aGlobalID();
}

cmodule2 WebPushKeyManager > DynCRUD<Entry> {
  start {
    registerBouncyCastle();
  }
  
  KeyPair generateKeyPair() ctex {
    ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(nl.martijndwars.webpush.Utils.CURVE);

    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(nl.martijndwars.webpush.Utils.ALGORITHM,
      org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
    keyPairGenerator.initialize(parameterSpec);

    ret keyPairGenerator.generateKeyPair();
  }
  
  enhanceFrame {
    internalFrameMenuItem(f, "Make key pair", rThreadEnter getKeyPair);
  }
  
  // API
  
  PKIKeyPair getKeyPair() {
    lock dbLock();
    
    Entry entry = conceptWhere Entry();
    if (entry == null) {
      KeyPair keyPair = generateKeyPair();
      ECPublicKey publicKey = cast keyPair.getPublic();
      ECPrivateKey privateKey = cast keyPair.getPrivate();

      byte[] encodedPublicKey = nl.martijndwars.webpush.Utils.encode(publicKey);
      byte[] encodedPrivateKey = nl.martijndwars.webpush.Utils.encode(privateKey);
    
      S publicKeyString = base64encode(encodedPublicKey);
      S privateKeyString = base64encode(encodedPrivateKey);   
      
      entry = cnew Entry(publicKey := publicKeyString,
        privateKey := SecretValue(privateKeyString));
    }
    
    ret PKIKeyPair(entry.privateKey!, entry.publicKey);
  }
  
  // subData = subscription data as received from e.g. mozilla.com
  void sendNotification(PKIKeyPair keyPair default getKeyPair(), MapSO subData, S msg) ctex {
    // not using PushAsyncService because lazy
    PushService pushService = new(keyPair.publicKey(),
      keyPair.privateKey());
    SS keys = cast subData.get("keys");
    Subscription subscription = new((S) subData.get("endpoint"),
      new Subscription.Keys(keys.get("p256dh"), keys.get("auth")));
    Notification notification = new(subscription, msg);
    pushService.send(notification);
    print("Sent push msg: " + msg);
  }
}

Author comment

Began life as a copy of #1029670

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030463
Snippet name: Web Push Manager
Eternal ID of this version: #1030463/13
Text MD5: fb8f7c214a81482c9780d51cfe193bf9
Transpilation MD5: 2e4be0befda347a70809cd3ccc90be46
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-21 17:57:43
Source code size: 2344 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 154 / 469
Version history: 12 change(s)
Referenced in: [show references]