Skip to content

Commit

Permalink
Capacitor: Android: LeapChat login and WS connection work!!! 🎉 miniwa…
Browse files Browse the repository at this point in the history
…re/miniware.go: Added comment; Capacitor: Added commented-out prod URLs

...for easing testing now and to add to conditional logic soon ™️
  • Loading branch information
elimisteve authored and jimmcgaw committed Mar 2, 2023
1 parent 8526d19 commit 9aa3e08
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
11 changes: 11 additions & 0 deletions miniware/miniware.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
HandshakeTimeout: 45 * time.Second,
CheckOrigin: func(r *http.Request) bool {
origin := r.Header.Get("Origin")
return origin == "http://127.0.0.1:8080" || // dev
origin == "http://localhost:8080" || // dev
origin == "http://10.0.2.2:8080" || // Android emulator
origin == "http://leapchat.org" || // prod
origin == "https://leapchat.org" || // prod
origin == "http://www.leapchat.org" || // prod
origin == "https://www.leapchat.org" || // prod
origin == "http://localhost" // Capacitor
},
}

func Auth(h http.Handler, m *Mapper) func(w http.ResponseWriter, req *http.Request) {
Expand Down
26 changes: 0 additions & 26 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,13 @@ class App extends Component {
this.onInitConnection(pincode);
};

<<<<<<< HEAD
createDeviceSession(passphrase) {
document.location.hash = '#' + passphrase;
}

onInitConnection(pincode='') {
const urlHash = document.location.hash + pincode;
this.props.initConnection(this.createDeviceSession, urlHash);
=======

onInitConnection(pincode='') {
this.props.initAuth();

let urlHash;
if (Capacitor.getPlatform() === 'web'){
urlHash = document.location.hash + pincode;
} else {
// for local testing in capacitor, just put in a valid fragment
urlHash = '';
}

initiateSessionAndConnect(
this.props.initConnection,
this.createWebSession,
urlHash,
);
}

createWebSession(passphrase) {
if (Capacitor.getPlatform() === "web"){
document.location.hash = "#" + passphrase;
}
>>>>>>> b63a58f (CapacitorJS testing WIP)
}

onToggleModalVisibility = (modalName, isVisible) => {
Expand Down
6 changes: 6 additions & 0 deletions src/store/epics/chatEpics.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import ChatHandler from './helpers/ChatHandler';
import { combineEpics } from 'redux-observable';
import createDetectVisibilityObservable from './helpers/createDetectPageVisibilityObservable';

// let wsUrl = `${window.location.origin.replace('http', 'ws')}/api/ws/messages/all`;
// if (Capacitor.isNativePlatform()) {
// // wsUrl = 'wss://www.leapchat.org/api/ws/messages/all'
// wsUrl = 'ws://10.0.2.2:8080/api/ws/messages/all'
// }

import { authUrl, wsUrl } from './helpers/urls';

export const chatHandler = new ChatHandler(wsUrl);
Expand Down
11 changes: 5 additions & 6 deletions src/store/epics/helpers/urls.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Capacitor } from '@capacitor/core';

let hostname;
if (typeof window.location !== "undefined") {
hostname = window.location.origin;
} else {
// hard-coded for mobile device scenarios
hostname = "http://locahost:8080";
let hostname = window.location.origin;

if (Capacitor.isNativePlatform()){
hostname = "http://10.0.2.2:8080";
}

export const authUrl = `${hostname}/api/login`;
Expand Down
24 changes: 17 additions & 7 deletions src/utils/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,41 @@ import { disconnected } from '../store/actions/chatActions';
// TODO: will be different host from a mobile device, probably if (!window)
let authUrl = `${window.location.origin}/api/login`;

if (Capacitor.isNativePlatform()){
authUrl = "http://192.168.1.247:8080/api/login";
if (Capacitor.isNativePlatform()) {
// authUrl = "https://www.leapchat.org/api/login";
authUrl = "http://10.0.2.2:8080/api/login";
}

// Helper for parsing response from Go API
//
// From https://stackoverflow.com/a/36183085
const b64toBlob = (base64, type = 'application/octet-stream') =>
fetch(`data:${type};base64,${base64}`).then(res => res.blob());

async function connectWithAuthRequest(initiateConnection, mID, secretKey, isNewPassphrase) {
let response;
if (Capacitor.isNativePlatform()){
let response, message;
if (Capacitor.isNativePlatform()) {
response = await CapacitorHttp.request({
url: authUrl,
headers: {
'X-Minilock-Id': mID
},
method: 'GET'
method: 'GET',
responseType: 'blob' // Makes `response.data` base64-encoded binary data
});

message = await b64toBlob(response.data);
} else {
response = await fetch(authUrl, {
method: "GET",
headers: {
'X-Minilock-Id': mID
}
});

message = await response.blob();
}

const message = await response.blob();

miniLock.crypto.decryptFile(message, mID, secretKey,
function (fileBlob, saveName, senderID) {
const reader = new FileReader();
Expand Down

0 comments on commit 9aa3e08

Please sign in to comment.