@xwiki/cristal-electron-authentication-github-main 0.21.1 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -3
- package/api-extractor.json +4 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.es.js +40 -49
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/etc/cristal-electron-authentication-github-main.api.md +14 -0
- package/package.json +12 -10
- package/src/index.ts +8 -1
- package/src/storage.ts +12 -22
- package/tsdoc.json +4 -0
- package/vitest.config.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
# @xwiki/cristal-electron-authentication-github-main
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## 0.22.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Cristal 0.22 Release
|
|
4
8
|
|
|
5
9
|
### Patch Changes
|
|
6
10
|
|
|
7
|
-
- Cristal 0.21.1 Release
|
|
8
11
|
- Updated dependencies
|
|
9
|
-
- @xwiki/cristal-authentication-api@0.
|
|
12
|
+
- @xwiki/cristal-authentication-api@0.22.0
|
|
10
13
|
|
|
11
14
|
## 0.21.0
|
|
12
15
|
|
package/dist/index.d.cts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import { BrowserWindow } from 'electron';
|
|
2
|
+
/**
|
|
3
|
+
* @param browserWindow - the browser window to use for the authentication process
|
|
4
|
+
* @param reload - the reload function
|
|
5
|
+
*
|
|
6
|
+
* @since 0.15
|
|
7
|
+
* @beta
|
|
8
|
+
*/
|
|
2
9
|
export declare function load(browserWindow: BrowserWindow, reload: (win: BrowserWindow) => void): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import { BrowserWindow } from 'electron';
|
|
2
|
+
/**
|
|
3
|
+
* @param browserWindow - the browser window to use for the authentication process
|
|
4
|
+
* @param reload - the reload function
|
|
5
|
+
*
|
|
6
|
+
* @since 0.15
|
|
7
|
+
* @beta
|
|
8
|
+
*/
|
|
2
9
|
export declare function load(browserWindow: BrowserWindow, reload: (win: BrowserWindow) => void): void;
|
package/dist/index.es.js
CHANGED
|
@@ -1,89 +1,80 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { ipcMain as
|
|
4
|
-
const p = "tokenType",
|
|
1
|
+
import y from "electron-store";
|
|
2
|
+
import m from "axios";
|
|
3
|
+
import { ipcMain as o } from "electron";
|
|
4
|
+
const p = "tokenType", g = "accessToken", v = {
|
|
5
5
|
tokenType: {
|
|
6
6
|
type: "string"
|
|
7
7
|
},
|
|
8
8
|
accessToken: {
|
|
9
9
|
type: "string"
|
|
10
10
|
}
|
|
11
|
-
},
|
|
11
|
+
}, e = new y({
|
|
12
12
|
name: "authentication-github-main",
|
|
13
|
-
schema:
|
|
13
|
+
schema: v
|
|
14
14
|
// TODO: add encryption key
|
|
15
15
|
});
|
|
16
|
-
function
|
|
17
|
-
|
|
16
|
+
function I(t) {
|
|
17
|
+
e.set(p, t);
|
|
18
18
|
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
function v(e) {
|
|
23
|
-
g.delete(e);
|
|
24
|
-
}
|
|
25
|
-
function w(e) {
|
|
26
|
-
d(p, e);
|
|
27
|
-
}
|
|
28
|
-
function A(e) {
|
|
29
|
-
d(f, e);
|
|
19
|
+
function b(t) {
|
|
20
|
+
e.set(g, t);
|
|
30
21
|
}
|
|
31
22
|
function l() {
|
|
32
|
-
return
|
|
23
|
+
return e.get(p);
|
|
33
24
|
}
|
|
34
25
|
function h() {
|
|
35
|
-
return
|
|
26
|
+
return e.get(g);
|
|
36
27
|
}
|
|
37
|
-
function
|
|
38
|
-
|
|
28
|
+
function _() {
|
|
29
|
+
e.delete(p);
|
|
39
30
|
}
|
|
40
|
-
function
|
|
41
|
-
|
|
31
|
+
function w() {
|
|
32
|
+
e.delete(g);
|
|
42
33
|
}
|
|
43
|
-
function
|
|
34
|
+
function A() {
|
|
44
35
|
return `${l()} ${h()}`;
|
|
45
36
|
}
|
|
46
|
-
function
|
|
47
|
-
|
|
37
|
+
function U(t, k) {
|
|
38
|
+
o.handle(
|
|
48
39
|
"authentication:github:login",
|
|
49
|
-
async (
|
|
50
|
-
baseUrl:
|
|
40
|
+
async (a, {
|
|
41
|
+
baseUrl: n,
|
|
51
42
|
deviceCode: i,
|
|
52
43
|
interval: c,
|
|
53
44
|
expiresIn: r
|
|
54
45
|
}) => {
|
|
55
|
-
const
|
|
56
|
-
|
|
46
|
+
const s = new URL(`${n}/device-verify`);
|
|
47
|
+
s.searchParams.set("device_code", i);
|
|
57
48
|
const T = parseInt(c) * 1e3 + 500;
|
|
58
|
-
let
|
|
59
|
-
const
|
|
60
|
-
const u = await (await fetch(
|
|
61
|
-
u.error ?
|
|
49
|
+
let d = parseInt(r) * 1e3;
|
|
50
|
+
const f = setInterval(async () => {
|
|
51
|
+
const u = await (await fetch(s)).json();
|
|
52
|
+
u.error ? d <= 0 && clearInterval(f) : (b(u.access_token), I(u.token_type), clearInterval(f), k(t)), d -= T;
|
|
62
53
|
}, T);
|
|
63
54
|
}
|
|
64
|
-
),
|
|
65
|
-
const
|
|
66
|
-
return
|
|
67
|
-
}),
|
|
55
|
+
), o.handle("authentication:github:isLoggedIn", () => {
|
|
56
|
+
const a = l(), n = h();
|
|
57
|
+
return a && n;
|
|
58
|
+
}), o.handle(
|
|
68
59
|
"authentication:github:userDetails",
|
|
69
60
|
async () => {
|
|
70
|
-
const
|
|
61
|
+
const a = "https://api.github.com/user", n = {
|
|
71
62
|
headers: {
|
|
72
|
-
Authorization:
|
|
63
|
+
Authorization: A()
|
|
73
64
|
}
|
|
74
65
|
}, {
|
|
75
|
-
data: { login: i, html_url: c, name: r, avatar_url:
|
|
76
|
-
} = await
|
|
77
|
-
return { profile: c, username: i, name: r, avatar:
|
|
66
|
+
data: { login: i, html_url: c, name: r, avatar_url: s }
|
|
67
|
+
} = await m.get(a, n);
|
|
68
|
+
return { profile: c, username: i, name: r, avatar: s };
|
|
78
69
|
}
|
|
79
|
-
),
|
|
70
|
+
), o.handle("authentication:github:authorizationValue", () => ({
|
|
80
71
|
tokenType: l(),
|
|
81
72
|
accessToken: h()
|
|
82
|
-
})),
|
|
83
|
-
|
|
73
|
+
})), o.handle("authentication:github:logout", () => {
|
|
74
|
+
w(), _();
|
|
84
75
|
});
|
|
85
76
|
}
|
|
86
77
|
export {
|
|
87
|
-
|
|
78
|
+
U as load
|
|
88
79
|
};
|
|
89
80
|
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/storage.ts","../src/index.ts"],"sourcesContent":["/**\n * See the LICENSE file distributed with this work for additional\n * information regarding copyright ownership.\n *\n * This is free software; you can redistribute it and/or modify it\n * under the terms of the GNU Lesser General Public License as\n * published by the Free Software Foundation; either version 2.1 of\n * the License, or (at your option) any later version.\n *\n * This software is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Lesser General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this software; if not, write to the Free\n * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA\n * 02110-1301 USA, or see the FSF site: http://www.fsf.org.\n */\n\nimport Store from \"electron-store\";\n\nconst tokenTypeKey = \"tokenType\";\n\nconst accessTokenKey = \"accessToken\";\n\nconst schema = {\n tokenType: {\n type: \"string\",\n },\n\n accessToken: {\n type: \"string\",\n },\n};\n\nconst storeInstance: Store = new Store({\n name: \"authentication-github-main\",\n schema,\n // TODO: add encryption key\n});\n\nfunction
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/storage.ts","../src/index.ts"],"sourcesContent":["/**\n * See the LICENSE file distributed with this work for additional\n * information regarding copyright ownership.\n *\n * This is free software; you can redistribute it and/or modify it\n * under the terms of the GNU Lesser General Public License as\n * published by the Free Software Foundation; either version 2.1 of\n * the License, or (at your option) any later version.\n *\n * This software is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Lesser General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this software; if not, write to the Free\n * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA\n * 02110-1301 USA, or see the FSF site: http://www.fsf.org.\n */\n\nimport Store from \"electron-store\";\n\nconst tokenTypeKey = \"tokenType\";\n\nconst accessTokenKey = \"accessToken\";\n\ntype StoreType = {\n tokenType: string;\n accessToken: string;\n};\n\nconst schema = {\n tokenType: {\n type: \"string\",\n },\n\n accessToken: {\n type: \"string\",\n },\n};\n\nconst storeInstance: Store<StoreType> = new Store<StoreType>({\n name: \"authentication-github-main\",\n schema,\n // TODO: add encryption key\n});\n\nfunction setTokenType(value: string): void {\n storeInstance.set(tokenTypeKey, value);\n}\n\nfunction setAccessToken(value: string): void {\n storeInstance.set(accessTokenKey, value);\n}\n\nfunction getTokenType(): string {\n return storeInstance.get(tokenTypeKey);\n}\n\nfunction getAccessToken(): string {\n return storeInstance.get(accessTokenKey);\n}\n\nfunction deleteTokenType(): void {\n storeInstance.delete(tokenTypeKey);\n}\n\nfunction deleteAccessToken(): void {\n storeInstance.delete(accessTokenKey);\n}\n\nexport {\n deleteAccessToken,\n deleteTokenType,\n getAccessToken,\n getTokenType,\n setAccessToken,\n setTokenType,\n};\n","/**\n * See the LICENSE file distributed with this work for additional\n * information regarding copyright ownership.\n *\n * This is free software; you can redistribute it and/or modify it\n * under the terms of the GNU Lesser General Public License as\n * published by the Free Software Foundation; either version 2.1 of\n * the License, or (at your option) any later version.\n *\n * This software is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Lesser General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this software; if not, write to the Free\n * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA\n * 02110-1301 USA, or see the FSF site: http://www.fsf.org.\n */\n\nimport {\n deleteAccessToken,\n deleteTokenType,\n getAccessToken,\n getTokenType,\n setAccessToken,\n setTokenType,\n} from \"./storage.js\";\nimport axios from \"axios\";\nimport { BrowserWindow, ipcMain } from \"electron\";\nimport type { UserDetails } from \"@xwiki/cristal-authentication-api\";\n\nfunction getAuthorizationValue() {\n return `${getTokenType()} ${getAccessToken()}`;\n}\n\n/**\n * @param browserWindow - the browser window to use for the authentication process\n * @param reload - the reload function\n *\n * @since 0.15\n * @beta\n */\nexport function load(\n browserWindow: BrowserWindow,\n reload: (win: BrowserWindow) => void,\n): void {\n ipcMain.handle(\n \"authentication:github:login\",\n async (\n _event,\n {\n baseUrl,\n deviceCode,\n interval,\n expiresIn,\n }: {\n baseUrl: string;\n deviceCode: string;\n interval: string;\n expiresIn: string;\n },\n ) => {\n const verificationUrl = new URL(`${baseUrl}/device-verify`);\n verificationUrl.searchParams.set(\"device_code\", deviceCode);\n\n // This converts the interval polling time and expiration time provided by\n // GitHub to milliseconds.\n // We need to add a little more delay to the interval when polling, just to\n // be sure that we don't go to fast for GitHub (or we might get rate\n // limited).\n const intervalMs: number = parseInt(interval) * 1000 + 500;\n let expiresInMs: number = parseInt(expiresIn) * 1000;\n\n // This interval handles polling the backend for the access token, using\n // the interval time computed earlier.\n // The backend will return an error until the login process has succeeded.\n const intervalId = setInterval(async () => {\n const response = await fetch(verificationUrl);\n const jsonResponse: {\n error?: string;\n access_token?: string;\n token_type?: string;\n } = await response.json();\n if (!jsonResponse.error) {\n setAccessToken(jsonResponse.access_token!);\n setTokenType(jsonResponse.token_type!);\n clearInterval(intervalId);\n // We reload the content on successful login.\n reload(browserWindow);\n } else if (expiresInMs <= 0) {\n clearInterval(intervalId);\n }\n expiresInMs -= intervalMs;\n }, intervalMs);\n },\n );\n\n ipcMain.handle(\"authentication:github:isLoggedIn\", () => {\n const tokenType = getTokenType();\n const accessTokenKey = getAccessToken();\n return tokenType && accessTokenKey;\n });\n\n ipcMain.handle(\n \"authentication:github:userDetails\",\n async (): Promise<UserDetails> => {\n const userinfoUrl = \"https://api.github.com/user\";\n const data = {\n headers: {\n Authorization: getAuthorizationValue(),\n },\n };\n const {\n data: { login, html_url, name, avatar_url },\n } = await axios.get(userinfoUrl, data);\n\n return { profile: html_url, username: login, name, avatar: avatar_url };\n },\n );\n ipcMain.handle(\"authentication:github:authorizationValue\", () => {\n return {\n tokenType: getTokenType(),\n accessToken: getAccessToken(),\n };\n });\n ipcMain.handle(\"authentication:github:logout\", () => {\n deleteAccessToken();\n deleteTokenType();\n });\n}\n"],"names":["tokenTypeKey","accessTokenKey","schema","storeInstance","Store","setTokenType","value","setAccessToken","getTokenType","getAccessToken","deleteTokenType","deleteAccessToken","getAuthorizationValue","load","browserWindow","reload","ipcMain","_event","baseUrl","deviceCode","interval","expiresIn","verificationUrl","intervalMs","expiresInMs","intervalId","jsonResponse","tokenType","userinfoUrl","data","login","html_url","name","avatar_url","axios"],"mappings":";;;AAsBA,MAAMA,IAAe,aAEfC,IAAiB,eAOjBC,IAAS;AAAA,EACb,WAAW;AAAA,IACT,MAAM;AAAA,EAAA;AAAA,EAGR,aAAa;AAAA,IACX,MAAM;AAAA,EAAA;AAEV,GAEMC,IAAkC,IAAIC,EAAiB;AAAA,EAC3D,MAAM;AAAA,EACN,QAAAF;AAAA;AAEF,CAAC;AAED,SAASG,EAAaC,GAAqB;AACzC,EAAAH,EAAc,IAAIH,GAAcM,CAAK;AACvC;AAEA,SAASC,EAAeD,GAAqB;AAC3C,EAAAH,EAAc,IAAIF,GAAgBK,CAAK;AACzC;AA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EI;AAAA,QACJ,MAAM,EAAE,OAAAkB,GAAO,UAAAC,GAAU,MAAAC,GAAM,YAAAC,EAAA;AAAA,MAAW,IACxC,MAAMC,EAAM,IAAIN,GAAaC,CAAI;AAErC,aAAO,EAAE,SAASE,GAAU,UAAUD,GAAO,MAAAE,GAAM,QAAQC,EAAA;AAAA,IAC7D;AAAA,EAAA,GAEFjB,EAAQ,OAAO,4CAA4C,OAClD;AAAA,IACL,WAAWR,EAAA;AAAA,IACX,aAAaC,EAAA;AAAA,EAAe,EAE/B,GACDO,EAAQ,OAAO,gCAAgC,MAAM;AACnD,IAAAL,EAAA,GACAD,EAAA;AAAA,EACF,CAAC;AACH;"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(e,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("electron-store"),require("axios"),require("electron")):typeof define=="function"&&define.amd?define(["exports","electron-store","axios","electron"],n):(e=typeof globalThis<"u"?globalThis:e||self,n(e["cristal_authentication-github-main"]={},e.Store,e.axios,e.electron))})(this,(function(e,n,v,i){"use strict";const r="tokenType",u="accessToken",m={tokenType:{type:"string"},accessToken:{type:"string"}},t=new n({name:"authentication-github-main",schema:m});function I(o){t.set(r,o)}function b(o){t.set(u,o)}function l(){return t.get(r)}function h(){return t.get(u)}function M(){t.delete(r)}function _(){t.delete(u)}function x(){return`${l()} ${h()}`}function w(o,A){i.ipcMain.handle("authentication:github:login",async(a,{baseUrl:s,deviceCode:p,interval:d,expiresIn:f})=>{const c=new URL(`${s}/device-verify`);c.searchParams.set("device_code",p);const T=parseInt(d)*1e3+500;let g=parseInt(f)*1e3;const k=setInterval(async()=>{const y=await(await fetch(c)).json();y.error?g<=0&&clearInterval(k):(b(y.access_token),I(y.token_type),clearInterval(k),A(o)),g-=T},T)}),i.ipcMain.handle("authentication:github:isLoggedIn",()=>{const a=l(),s=h();return a&&s}),i.ipcMain.handle("authentication:github:userDetails",async()=>{const a="https://api.github.com/user",s={headers:{Authorization:x()}},{data:{login:p,html_url:d,name:f,avatar_url:c}}=await v.get(a,s);return{profile:d,username:p,name:f,avatar:c}}),i.ipcMain.handle("authentication:github:authorizationValue",()=>({tokenType:l(),accessToken:h()})),i.ipcMain.handle("authentication:github:logout",()=>{_(),M()})}e.load=w,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/storage.ts","../src/index.ts"],"sourcesContent":["/**\n * See the LICENSE file distributed with this work for additional\n * information regarding copyright ownership.\n *\n * This is free software; you can redistribute it and/or modify it\n * under the terms of the GNU Lesser General Public License as\n * published by the Free Software Foundation; either version 2.1 of\n * the License, or (at your option) any later version.\n *\n * This software is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Lesser General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this software; if not, write to the Free\n * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA\n * 02110-1301 USA, or see the FSF site: http://www.fsf.org.\n */\n\nimport Store from \"electron-store\";\n\nconst tokenTypeKey = \"tokenType\";\n\nconst accessTokenKey = \"accessToken\";\n\nconst schema = {\n tokenType: {\n type: \"string\",\n },\n\n accessToken: {\n type: \"string\",\n },\n};\n\nconst storeInstance: Store = new Store({\n name: \"authentication-github-main\",\n schema,\n // TODO: add encryption key\n});\n\nfunction
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/storage.ts","../src/index.ts"],"sourcesContent":["/**\n * See the LICENSE file distributed with this work for additional\n * information regarding copyright ownership.\n *\n * This is free software; you can redistribute it and/or modify it\n * under the terms of the GNU Lesser General Public License as\n * published by the Free Software Foundation; either version 2.1 of\n * the License, or (at your option) any later version.\n *\n * This software is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Lesser General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this software; if not, write to the Free\n * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA\n * 02110-1301 USA, or see the FSF site: http://www.fsf.org.\n */\n\nimport Store from \"electron-store\";\n\nconst tokenTypeKey = \"tokenType\";\n\nconst accessTokenKey = \"accessToken\";\n\ntype StoreType = {\n tokenType: string;\n accessToken: string;\n};\n\nconst schema = {\n tokenType: {\n type: \"string\",\n },\n\n accessToken: {\n type: \"string\",\n },\n};\n\nconst storeInstance: Store<StoreType> = new Store<StoreType>({\n name: \"authentication-github-main\",\n schema,\n // TODO: add encryption key\n});\n\nfunction setTokenType(value: string): void {\n storeInstance.set(tokenTypeKey, value);\n}\n\nfunction setAccessToken(value: string): void {\n storeInstance.set(accessTokenKey, value);\n}\n\nfunction getTokenType(): string {\n return storeInstance.get(tokenTypeKey);\n}\n\nfunction getAccessToken(): string {\n return storeInstance.get(accessTokenKey);\n}\n\nfunction deleteTokenType(): void {\n storeInstance.delete(tokenTypeKey);\n}\n\nfunction deleteAccessToken(): void {\n storeInstance.delete(accessTokenKey);\n}\n\nexport {\n deleteAccessToken,\n deleteTokenType,\n getAccessToken,\n getTokenType,\n setAccessToken,\n setTokenType,\n};\n","/**\n * See the LICENSE file distributed with this work for additional\n * information regarding copyright ownership.\n *\n * This is free software; you can redistribute it and/or modify it\n * under the terms of the GNU Lesser General Public License as\n * published by the Free Software Foundation; either version 2.1 of\n * the License, or (at your option) any later version.\n *\n * This software is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Lesser General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this software; if not, write to the Free\n * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA\n * 02110-1301 USA, or see the FSF site: http://www.fsf.org.\n */\n\nimport {\n deleteAccessToken,\n deleteTokenType,\n getAccessToken,\n getTokenType,\n setAccessToken,\n setTokenType,\n} from \"./storage.js\";\nimport axios from \"axios\";\nimport { BrowserWindow, ipcMain } from \"electron\";\nimport type { UserDetails } from \"@xwiki/cristal-authentication-api\";\n\nfunction getAuthorizationValue() {\n return `${getTokenType()} ${getAccessToken()}`;\n}\n\n/**\n * @param browserWindow - the browser window to use for the authentication process\n * @param reload - the reload function\n *\n * @since 0.15\n * @beta\n */\nexport function load(\n browserWindow: BrowserWindow,\n reload: (win: BrowserWindow) => void,\n): void {\n ipcMain.handle(\n \"authentication:github:login\",\n async (\n _event,\n {\n baseUrl,\n deviceCode,\n interval,\n expiresIn,\n }: {\n baseUrl: string;\n deviceCode: string;\n interval: string;\n expiresIn: string;\n },\n ) => {\n const verificationUrl = new URL(`${baseUrl}/device-verify`);\n verificationUrl.searchParams.set(\"device_code\", deviceCode);\n\n // This converts the interval polling time and expiration time provided by\n // GitHub to milliseconds.\n // We need to add a little more delay to the interval when polling, just to\n // be sure that we don't go to fast for GitHub (or we might get rate\n // limited).\n const intervalMs: number = parseInt(interval) * 1000 + 500;\n let expiresInMs: number = parseInt(expiresIn) * 1000;\n\n // This interval handles polling the backend for the access token, using\n // the interval time computed earlier.\n // The backend will return an error until the login process has succeeded.\n const intervalId = setInterval(async () => {\n const response = await fetch(verificationUrl);\n const jsonResponse: {\n error?: string;\n access_token?: string;\n token_type?: string;\n } = await response.json();\n if (!jsonResponse.error) {\n setAccessToken(jsonResponse.access_token!);\n setTokenType(jsonResponse.token_type!);\n clearInterval(intervalId);\n // We reload the content on successful login.\n reload(browserWindow);\n } else if (expiresInMs <= 0) {\n clearInterval(intervalId);\n }\n expiresInMs -= intervalMs;\n }, intervalMs);\n },\n );\n\n ipcMain.handle(\"authentication:github:isLoggedIn\", () => {\n const tokenType = getTokenType();\n const accessTokenKey = getAccessToken();\n return tokenType && accessTokenKey;\n });\n\n ipcMain.handle(\n \"authentication:github:userDetails\",\n async (): Promise<UserDetails> => {\n const userinfoUrl = \"https://api.github.com/user\";\n const data = {\n headers: {\n Authorization: getAuthorizationValue(),\n },\n };\n const {\n data: { login, html_url, name, avatar_url },\n } = await axios.get(userinfoUrl, data);\n\n return { profile: html_url, username: login, name, avatar: avatar_url };\n },\n );\n ipcMain.handle(\"authentication:github:authorizationValue\", () => {\n return {\n tokenType: getTokenType(),\n accessToken: getAccessToken(),\n };\n });\n ipcMain.handle(\"authentication:github:logout\", () => {\n deleteAccessToken();\n deleteTokenType();\n });\n}\n"],"names":["tokenTypeKey","accessTokenKey","schema","storeInstance","Store","setTokenType","value","setAccessToken","getTokenType","getAccessToken","deleteTokenType","deleteAccessToken","getAuthorizationValue","load","browserWindow","reload","ipcMain","_event","baseUrl","deviceCode","interval","expiresIn","verificationUrl","intervalMs","expiresInMs","intervalId","jsonResponse","tokenType","userinfoUrl","data","login","html_url","name","avatar_url","axios"],"mappings":"kYAsBA,MAAMA,EAAe,YAEfC,EAAiB,cAOjBC,EAAS,CACb,UAAW,CACT,KAAM,QAAA,EAGR,YAAa,CACX,KAAM,QAAA,CAEV,EAEMC,EAAkC,IAAIC,EAAiB,CAC3D,KAAM,6BACN,OAAAF,CAEF,CAAC,EAED,SAASG,EAAaC,EAAqB,CACzCH,EAAc,IAAIH,EAAcM,CAAK,CACvC,CAEA,SAASC,EAAeD,EAAqB,CAC3CH,EAAc,IAAIF,EAAgBK,CAAK,CACzC,CAEA,SAASE,GAAuB,CAC9B,OAAOL,EAAc,IAAIH,CAAY,CACvC,CAEA,SAASS,GAAyB,CAChC,OAAON,EAAc,IAAIF,CAAc,CACzC,CAEA,SAASS,GAAwB,CAC/BP,EAAc,OAAOH,CAAY,CACnC,CAEA,SAASW,GAA0B,CACjCR,EAAc,OAAOF,CAAc,CACrC,CCrCA,SAASW,GAAwB,CAC/B,MAA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
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
## API Report File for "@xwiki/cristal-electron-authentication-github-main"
|
|
2
|
+
|
|
3
|
+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
|
|
7
|
+
import { BrowserWindow } from 'electron';
|
|
8
|
+
|
|
9
|
+
// @beta (undocumented)
|
|
10
|
+
export function load(browserWindow: BrowserWindow, reload: (win: BrowserWindow) => void): void;
|
|
11
|
+
|
|
12
|
+
// (No @packageDocumentation comment for this package)
|
|
13
|
+
|
|
14
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xwiki/cristal-electron-authentication-github-main",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"license": "LGPL 2.1",
|
|
5
5
|
"author": "XWiki Org Community <contact@xwiki.org>",
|
|
6
6
|
"homepage": "https://cristal.xwiki.org/",
|
|
@@ -26,24 +26,26 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"main": "./dist/index.es.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"axios": "1.
|
|
31
|
+
"axios": "1.12.0",
|
|
31
32
|
"electron-store": "10.1.0",
|
|
32
|
-
"@xwiki/cristal-authentication-api": "0.
|
|
33
|
+
"@xwiki/cristal-authentication-api": "0.22.0"
|
|
33
34
|
},
|
|
34
35
|
"peerDependencies": {
|
|
35
|
-
"electron": "37.
|
|
36
|
+
"electron": "37.4.0"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"typescript": "5.9.2",
|
|
39
|
-
"vite": "7.
|
|
40
|
-
"@xwiki/cristal-dev-config": "0.
|
|
40
|
+
"vite": "7.1.5",
|
|
41
|
+
"@xwiki/cristal-dev-config": "0.22.0"
|
|
41
42
|
},
|
|
42
43
|
"scripts": {
|
|
43
|
-
"
|
|
44
|
+
"api-extractor:local": "api-extractor run --local",
|
|
45
|
+
"build": "vite build",
|
|
44
46
|
"clean": "rimraf dist",
|
|
45
47
|
"lint": "eslint \"./src/**/*.{ts,tsx,vue}\" --max-warnings=0",
|
|
46
|
-
"test": "vitest --run"
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
"test": "vitest --run",
|
|
49
|
+
"typecheck": "tsc"
|
|
50
|
+
}
|
|
49
51
|
}
|
package/src/index.ts
CHANGED
|
@@ -26,14 +26,21 @@ import {
|
|
|
26
26
|
setAccessToken,
|
|
27
27
|
setTokenType,
|
|
28
28
|
} from "./storage.js";
|
|
29
|
-
import { UserDetails } from "@xwiki/cristal-authentication-api";
|
|
30
29
|
import axios from "axios";
|
|
31
30
|
import { BrowserWindow, ipcMain } from "electron";
|
|
31
|
+
import type { UserDetails } from "@xwiki/cristal-authentication-api";
|
|
32
32
|
|
|
33
33
|
function getAuthorizationValue() {
|
|
34
34
|
return `${getTokenType()} ${getAccessToken()}`;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* @param browserWindow - the browser window to use for the authentication process
|
|
39
|
+
* @param reload - the reload function
|
|
40
|
+
*
|
|
41
|
+
* @since 0.15
|
|
42
|
+
* @beta
|
|
43
|
+
*/
|
|
37
44
|
export function load(
|
|
38
45
|
browserWindow: BrowserWindow,
|
|
39
46
|
reload: (win: BrowserWindow) => void,
|
package/src/storage.ts
CHANGED
|
@@ -24,6 +24,11 @@ const tokenTypeKey = "tokenType";
|
|
|
24
24
|
|
|
25
25
|
const accessTokenKey = "accessToken";
|
|
26
26
|
|
|
27
|
+
type StoreType = {
|
|
28
|
+
tokenType: string;
|
|
29
|
+
accessToken: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
27
32
|
const schema = {
|
|
28
33
|
tokenType: {
|
|
29
34
|
type: "string",
|
|
@@ -34,49 +39,34 @@ const schema = {
|
|
|
34
39
|
},
|
|
35
40
|
};
|
|
36
41
|
|
|
37
|
-
const storeInstance: Store = new Store({
|
|
42
|
+
const storeInstance: Store<StoreType> = new Store<StoreType>({
|
|
38
43
|
name: "authentication-github-main",
|
|
39
44
|
schema,
|
|
40
45
|
// TODO: add encryption key
|
|
41
46
|
});
|
|
42
47
|
|
|
43
|
-
function set<T>(key: string, value: T) {
|
|
44
|
-
// @ts-expect-error type resolution failing because of electron-store library bug
|
|
45
|
-
storeInstance.set(key, value);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function get<T>(key: string): T {
|
|
49
|
-
// @ts-expect-error type resolution failing because of electron-store library bug
|
|
50
|
-
return storeInstance.get(key) as T;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function rm(key: string) {
|
|
54
|
-
// @ts-expect-error type resolution failing because of electron-store library bug
|
|
55
|
-
storeInstance.delete(key);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
48
|
function setTokenType(value: string): void {
|
|
59
|
-
set(tokenTypeKey, value);
|
|
49
|
+
storeInstance.set(tokenTypeKey, value);
|
|
60
50
|
}
|
|
61
51
|
|
|
62
52
|
function setAccessToken(value: string): void {
|
|
63
|
-
set(accessTokenKey, value);
|
|
53
|
+
storeInstance.set(accessTokenKey, value);
|
|
64
54
|
}
|
|
65
55
|
|
|
66
56
|
function getTokenType(): string {
|
|
67
|
-
return get(tokenTypeKey);
|
|
57
|
+
return storeInstance.get(tokenTypeKey);
|
|
68
58
|
}
|
|
69
59
|
|
|
70
60
|
function getAccessToken(): string {
|
|
71
|
-
return get(accessTokenKey);
|
|
61
|
+
return storeInstance.get(accessTokenKey);
|
|
72
62
|
}
|
|
73
63
|
|
|
74
64
|
function deleteTokenType(): void {
|
|
75
|
-
|
|
65
|
+
storeInstance.delete(tokenTypeKey);
|
|
76
66
|
}
|
|
77
67
|
|
|
78
68
|
function deleteAccessToken(): void {
|
|
79
|
-
|
|
69
|
+
storeInstance.delete(accessTokenKey);
|
|
80
70
|
}
|
|
81
71
|
|
|
82
72
|
export {
|
package/tsdoc.json
ADDED
package/vitest.config.ts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
import localConfig from "./vite.config";
|
|
22
|
+
import { vitestVue as defaultConfig } from "@xwiki/cristal-dev-config";
|
|
22
23
|
import { mergeConfig } from "vitest/config";
|
|
23
|
-
import defaultConfig from "@xwiki/cristal-dev-config/vitest-vue.config";
|
|
24
24
|
|
|
25
25
|
export default mergeConfig(defaultConfig, localConfig);
|