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).

1  
!7
2  
3  
!include once #1030462 // BouncyCastle
4  
5  
import nl.martijndwars.webpush.Subscription;
6  
import nl.martijndwars.webpush.PushService;
7  
import nl.martijndwars.webpush.Notification;
8  
9  
concept Entry {
10  
  S publicKey;
11  
  SecretValue<S> privateKey;
12  
  S comment;
13  
  S globalID = aGlobalID();
14  
}
15  
16  
cmodule2 WebPushKeyManager > DynCRUD<Entry> {
17  
  start {
18  
    registerBouncyCastle();
19  
  }
20  
  
21  
  KeyPair generateKeyPair() ctex {
22  
    ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(nl.martijndwars.webpush.Utils.CURVE);
23  
24  
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(nl.martijndwars.webpush.Utils.ALGORITHM,
25  
      org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
26  
    keyPairGenerator.initialize(parameterSpec);
27  
28  
    ret keyPairGenerator.generateKeyPair();
29  
  }
30  
  
31  
  enhanceFrame {
32  
    internalFrameMenuItem(f, "Make key pair", rThreadEnter getKeyPair);
33  
  }
34  
  
35  
  // API
36  
  
37  
  PKIKeyPair getKeyPair() {
38  
    lock dbLock();
39  
    
40  
    Entry entry = conceptWhere Entry();
41  
    if (entry == null) {
42  
      KeyPair keyPair = generateKeyPair();
43  
      ECPublicKey publicKey = cast keyPair.getPublic();
44  
      ECPrivateKey privateKey = cast keyPair.getPrivate();
45  
46  
      byte[] encodedPublicKey = nl.martijndwars.webpush.Utils.encode(publicKey);
47  
      byte[] encodedPrivateKey = nl.martijndwars.webpush.Utils.encode(privateKey);
48  
    
49  
      S publicKeyString = base64encode(encodedPublicKey);
50  
      S privateKeyString = base64encode(encodedPrivateKey);   
51  
      
52  
      entry = cnew Entry(publicKey := publicKeyString,
53  
        privateKey := SecretValue(privateKeyString));
54  
    }
55  
    
56  
    ret PKIKeyPair(entry.privateKey!, entry.publicKey);
57  
  }
58  
  
59  
  // subData = subscription data as received from e.g. mozilla.com
60  
  void sendNotification(PKIKeyPair keyPair default getKeyPair(), MapSO subData, S msg) ctex {
61  
    // not using PushAsyncService because lazy
62  
    PushService pushService = new(keyPair.publicKey(),
63  
      keyPair.privateKey());
64  
    SS keys = cast subData.get("keys");
65  
    Subscription subscription = new((S) subData.get("endpoint"),
66  
      new Subscription.Keys(keys.get("p256dh"), keys.get("auth")));
67  
    Notification notification = new(subscription, msg);
68  
    pushService.send(notification);
69  
    print("Sent push msg: " + msg);
70  
  }
71  
}

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: 248 / 621
Version history: 12 change(s)
Referenced in: [show references]