Skip to content

Commit

Permalink
feat(cookies): add get cookies plugin method
Browse files Browse the repository at this point in the history
* feat(cookies): add get cookies plugin method

* chore: add missing import

* chore: run fmt
  • Loading branch information
ItsChaceD authored Nov 15, 2022
1 parent 9f3c27d commit ba1e770
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.webkit.JavascriptInterface;
import androidx.annotation.Nullable;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginConfig;
Expand Down Expand Up @@ -91,6 +92,19 @@ public void setCookie(String key, String value) {
}
}

@PluginMethod
public void getCookies(PluginCall call) {
String url = getServerUrl(call);
if (!url.isEmpty()) {
JSObject cookiesMap = new JSObject();
HttpCookie[] cookies = cookieManager.getCookies(url);
for (HttpCookie cookie : cookies) {
cookiesMap.put(cookie.getName(), cookie.getValue());
}
call.resolve(cookiesMap);
}
}

@PluginMethod
public void setCookie(PluginCall call) {
String key = call.getString("key");
Expand Down
27 changes: 27 additions & 0 deletions core/src/core-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ const encode = (str: string) =>
.replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
.replace(/[()]/g, escape);

/**
* Safely web decode a string value (inspired by js-cookie)
* @param str The string value to decode
*/
const decode = (str: string): string =>
str.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);

export interface CapacitorCookiesPlugin {
getCookies(options?: GetCookieOptions): Promise<HttpCookieMap>;
setCookie(options: SetCookieOptions): Promise<void>;
deleteCookie(options: DeleteCookieOptions): Promise<void>;
clearCookies(options: ClearCookieOptions): Promise<void>;
Expand All @@ -39,11 +47,16 @@ interface HttpCookie {
value: string;
}

interface HttpCookieMap {
[key: string]: string;
}

interface HttpCookieExtras {
path?: string;
expires?: string;
}

export type GetCookieOptions = Omit<HttpCookie, 'key' | 'value'>;
export type SetCookieOptions = HttpCookie & HttpCookieExtras;
export type DeleteCookieOptions = Omit<HttpCookie, 'value'>;
export type ClearCookieOptions = Omit<HttpCookie, 'key' | 'value'>;
Expand All @@ -52,6 +65,20 @@ export class CapacitorCookiesPluginWeb
extends WebPlugin
implements CapacitorCookiesPlugin
{
async getCookies(): Promise<HttpCookieMap> {
const cookies = document.cookie;
const cookieMap: HttpCookieMap = {};
cookies.split(';').forEach(cookie => {
if (cookie.length <= 0) return;
// Replace first "=" with CAP_COOKIE to prevent splitting on additional "="
let [key, value] = cookie.replace(/=/, 'CAP_COOKIE').split('CAP_COOKIE');
key = decode(key).trim();
value = decode(value).trim();
cookieMap[key] = value;
});
return cookieMap;
}

async setCookie(options: SetCookieOptions): Promise<void> {
try {
// Safely Encoded Key/Value
Expand Down
11 changes: 11 additions & 0 deletions ios/Capacitor/Capacitor/Plugins/CapacitorCookieManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public class CapacitorCookieManager {
jar.setCookies(cookies, for: url, mainDocumentURL: url)
}

public func getCookiesAsMap(_ url: URL) -> [String: String] {
var cookiesMap: [String: String] = [:]
let jar = HTTPCookieStorage.shared
if let cookies = jar.cookies(for: url) {
for cookie in cookies {
cookiesMap[cookie.name] = cookie.value
}
}
return cookiesMap
}

public func getCookies() -> String {
let jar = HTTPCookieStorage.shared
guard let url = self.getServerUrl() else { return "" }
Expand Down
22 changes: 8 additions & 14 deletions ios/Capacitor/Capacitor/Plugins/CapacitorCookies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class CAPCookiesPlugin: CAPPlugin {
cookieManager = CapacitorCookieManager(bridge?.config)
}

@objc func getCookies(_ call: CAPPluginCall) {
guard let url = cookieManager!.getServerUrl(call) else { return call.reject("Invalid URL / Server URL")}
call.resolve(cookieManager!.getCookiesAsMap(url))
}

@objc func setCookie(_ call: CAPPluginCall) {
guard let key = call.getString("key") else { return call.reject("Must provide key") }
guard let value = call.getString("value") else { return call.reject("Must provide value") }
Expand All @@ -21,20 +26,9 @@ public class CAPCookiesPlugin: CAPPlugin {

@objc func deleteCookie(_ call: CAPPluginCall) {
guard let key = call.getString("key") else { return call.reject("Must provide key") }
let url = cookieManager!.getServerUrl(call)
if url != nil {
let jar = HTTPCookieStorage.shared

let cookie = jar.cookies(for: url!)?.first(where: { (cookie) -> Bool in
return cookie.name == key
})

if cookie != nil {
jar.deleteCookie(cookie!)
}

call.resolve()
}
guard let url = cookieManager!.getServerUrl(call) else { return call.reject("Invalid URL / Server URL")}
cookieManager!.deleteCookie(url, key)
call.resolve()
}

@objc func clearCookies(_ call: CAPPluginCall) {
Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "CAPBridgedPlugin.h"

CAP_PLUGIN(CAPCookiesPlugin, "CapacitorCookies",
CAP_PLUGIN_METHOD(getCookies, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setCookie, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(deleteCookie, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(clearCookies, CAPPluginReturnPromise);
Expand Down

0 comments on commit ba1e770

Please sign in to comment.