Skip to content

Commit 0b3942b

Browse files
Merge pull request #69 from NatLibFi/ekir-magazines
EKIR-754 Handle opening of magazines
2 parents 2ea130b + ebc4a96 commit 0b3942b

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

src/auth/ekirjastoFetch.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ export async function fetchEAuthToken(
88
url: string | undefined,
99
token: string | undefined
1010
) {
11-
if (!url) {
11+
if (!url || !token) {
1212
throw new ApplicationError({
1313
title: "Incomplete Authentication Info",
1414
detail: "No URL or Token was provided for authentication"
1515
});
1616
}
1717

18+
//If in some case, the bearer text is present, remove it so there is no repetition in the request
19+
if (token?.startsWith("Bearer ")) {
20+
token = token.replace("Bearer ", "");
21+
}
22+
1823
const response = await fetchWithHeaders(url, `Bearer ${token}`, {}, "POST");
1924
const json = await response.json();
2025

@@ -24,3 +29,24 @@ export async function fetchEAuthToken(
2429

2530
return json;
2631
}
32+
33+
export async function fetchEkirjastoToken(
34+
url: string | undefined,
35+
token: string | undefined
36+
) {
37+
if (!url || !token) {
38+
throw new ApplicationError({
39+
title: "Incomplete Authentication Info",
40+
detail: "No URL or Token was provided for authentication"
41+
});
42+
}
43+
44+
const response = await fetchWithHeaders(url, token, {}, "GET");
45+
const json = await response.json();
46+
47+
if (!response.ok) {
48+
throw new ServerError(url, response.status, json);
49+
}
50+
51+
return json;
52+
}

src/auth/useCredentials.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ function getCredentialsCookie(
113113
if (librarySlug === "ekirjasto") {
114114
// Get access token, for ekirjasto login credentials
115115
const accessToken = Cookie.get(cookieNameEkirjasto());
116+
if (!accessToken) {
117+
console.log("No access token");
118+
return undefined;
119+
}
116120
// Create ekirjasto authentication credentials
117121
const authCredentials: AuthCredentials = {
118122
token: `Bearer ${accessToken}`,

src/components/context/UserContext.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as React from "react";
88
import useSWR from "swr";
99
import { BasicTokenAuthType, EkirjastoAuthType } from "types/opds1";
1010
import { addHours, isBefore } from "date-fns";
11+
import { fetchEkirjastoToken } from "auth/ekirjastoFetch";
1112

1213
type Status = "authenticated" | "loading" | "unauthenticated";
1314
export type UserState = {
@@ -22,6 +23,10 @@ export type UserState = {
2223
authenticationUrl: string | undefined
2324
) => void;
2425
signOut: () => void;
26+
getEkirjastoToken: (
27+
token: string,
28+
fetchUrl: string | undefined
29+
) => Promise<string>;
2530
setBook: (book: AnyBook, id?: string) => void;
2631
error: any;
2732
token: string | undefined;
@@ -117,6 +122,17 @@ export const UserProvider = ({ children }: UserProviderProps) => {
117122
}
118123
);
119124

125+
async function getEkirjastoToken(
126+
token: string,
127+
fetchUrl: string | undefined
128+
): Promise<string> {
129+
const { token: ekirjastoToken } = await fetchEkirjastoToken(
130+
fetchUrl,
131+
token
132+
);
133+
return ekirjastoToken;
134+
}
135+
120136
function signIn(
121137
token: string | Token,
122138
method: AppAuthMethod,
@@ -160,6 +176,7 @@ export const UserProvider = ({ children }: UserProviderProps) => {
160176
refetchLoans: mutate,
161177
signIn,
162178
signOut,
179+
getEkirjastoToken,
163180
setBook,
164181
error,
165182
token: stringifyToken(credentials),

src/config/magazines.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ export const MAGAZINE_CONFIG = {
7373

7474
// Default iframe sandbox permissions
7575
IFRAME_SANDBOX:
76-
"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"
76+
"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-top-navigation-by-user-activation"
7777
} as const;

src/pages/[library]/magazines/index.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,32 @@ import {
1717
} from "config/magazines";
1818
import Head from "next/head";
1919
import BreadcrumbBar from "components/BreadcrumbBar";
20+
import { EKIRJASTO_AUTH_TYPE } from "utils/constants";
2021

2122
const MagazinesFixedContent: React.FC = () => {
2223
const iframeRef = React.useRef<HTMLIFrameElement | null>(null);
23-
const { token } = useUser();
24+
const { token, getEkirjastoToken } = useUser();
2425
const { initLogin } = useLogin();
25-
const { slug } = useLibraryContext();
26+
const { slug, authMethods } = useLibraryContext();
27+
const ekirMethod = authMethods.find(
28+
method => method.type === EKIRJASTO_AUTH_TYPE
29+
);
30+
let ekirjastoToken: string | undefined;
31+
if (ekirMethod && token) {
32+
try {
33+
//Get the ekirjastoToken
34+
const ekirjastoTokenUrl = ekirMethod.links.find(
35+
link => link.rel === "ekirjasto_token"
36+
)?.href;
37+
ekirjastoToken = getEkirjastoToken(token, ekirjastoTokenUrl);
38+
} catch (error) {
39+
//Can not start the reader so should show not logged in or something
40+
}
41+
}
42+
if (!token) {
43+
console.log("There is no token so should be logged out");
44+
ekirjastoToken = undefined;
45+
}
2646

2747
const storageKey = React.useMemo(
2848
() => `${MAGAZINE_CONFIG.STORAGE_KEY_PREFIX}${slug ?? "default"}`,
@@ -45,12 +65,17 @@ const MagazinesFixedContent: React.FC = () => {
4565
const handleMessage = React.useCallback(
4666
(e: MessageEvent) => {
4767
const allowedOrigin = getMagazineAllowedOrigin();
68+
69+
if (!token) {
70+
console.log("No token!");
71+
}
72+
4873
if (e.origin !== allowedOrigin || typeof e.data !== "string") return;
4974

5075
if (e.data === "ewl:unauthorized") {
51-
if (token) {
76+
if (ekirjastoToken) {
5277
iframeRef.current?.contentWindow?.postMessage(
53-
`ewl:login:${token}`,
78+
`ewl:login:${ekirjastoToken}`,
5479
allowedOrigin
5580
);
5681
} else {
@@ -72,7 +97,7 @@ const MagazinesFixedContent: React.FC = () => {
7297
});
7398
}
7499
},
75-
[initLogin, token, storageKey]
100+
[initLogin, ekirjastoToken, token, storageKey]
76101
);
77102

78103
React.useEffect(() => {

0 commit comments

Comments
 (0)