11import * as React from "react" ;
22import Cookie from "js-cookie" ;
3- import { AuthCredentials , OPDS1 } from "interfaces" ;
3+ import { AppAuthMethod , AuthCredentials , OPDS1 } from "interfaces" ;
44import { IS_SERVER } from "utils/env" ;
55import { NextRouter , useRouter } from "next/router" ;
66import { generateCredentials } from "utils/auth" ;
7- import { SAML_LOGIN_QUERY_PARAM } from "utils/constants" ;
7+ import {
8+ EKIRJASTO_AUTH_TYPE ,
9+ EKIRJASTO_TOKEN_PARAM ,
10+ SAML_LOGIN_QUERY_PARAM
11+ } from "utils/constants" ;
812
913/**
1014 * This hook:
@@ -16,16 +20,31 @@ import { SAML_LOGIN_QUERY_PARAM } from "utils/constants";
1620 * if finds a token, it extracts it and sets it as the current
1721 * credentials.
1822 */
19- export default function useCredentials ( slug : string | null ) {
23+ export default function useCredentials (
24+ slug : string | null ,
25+ authMethods : AppAuthMethod [ ] | null
26+ ) {
2027 const router = useRouter ( ) ;
28+
29+ // Since we don't actually call the login function anywhere, we need to put the authentication url
30+ // in somehow, so we fetch it here and take it to getCredentialsState function
31+ const ekirjastoMethod = authMethods ?. find (
32+ method => method . type === EKIRJASTO_AUTH_TYPE
33+ ) ;
34+ let authenticationUrl ;
35+ if ( ekirjastoMethod ) {
36+ authenticationUrl = ekirjastoMethod . links ?. find (
37+ link => link . rel === "authenticate"
38+ ) ?. href ;
39+ }
2140 const [ credentialsState , setCredentialsState ] = React . useState <
2241 AuthCredentials | undefined
23- > ( getCredentialsCookie ( slug ) ) ;
42+ > ( getCredentialsCookie ( slug , authenticationUrl ) ) ;
2443 // sync up cookie state with react state
2544 React . useEffect ( ( ) => {
26- const cookie = getCredentialsCookie ( slug ) ;
45+ const cookie = getCredentialsCookie ( slug , authenticationUrl ) ;
2746 if ( cookie ) setCredentialsState ( cookie ) ;
28- } , [ slug ] ) ;
47+ } , [ authenticationUrl , slug ] ) ;
2948
3049 // set both cookie and state credentials
3150 const setCredentials = React . useCallback (
@@ -60,7 +79,7 @@ export default function useCredentials(slug: string | null) {
6079}
6180
6281/**
63- * COOKIE CREDENDIALS
82+ * COOKIE CREDENTIALS
6483 */
6584/**
6685 * If you pass a librarySlug, the cookie will be scoped to the
@@ -70,31 +89,69 @@ function cookieName(librarySlug: string | null): string {
7089 const AUTH_COOKIE_NAME = "CPW_AUTH_COOKIE" ;
7190 return `${ AUTH_COOKIE_NAME } /${ librarySlug } ` ;
7291}
73-
92+ /**
93+ * When using ekirjasto authentication, we don't use scoping to a particular library
94+ * @returns ekirjasto cookie name
95+ */
96+ function cookieNameEkirjasto ( ) : string {
97+ const AUTH_COOKIE_NAME = EKIRJASTO_TOKEN_PARAM ;
98+ return AUTH_COOKIE_NAME ;
99+ }
100+ /**
101+ * Get credentials from cookies.
102+ * We assume the token is in access_token cookie, and that it is
103+ * of the Ekirjasto Authentication type
104+ *
105+ * @param librarySlug Library slug, that is useful if we have multiple libraries
106+ * @param authenticationUrl AuthenticationUrl where we make refresh requests
107+ * @returns Ekirjasto credentials if access_token is available, otherwise undefined
108+ */
74109function getCredentialsCookie (
75- librarySlug : string | null
110+ librarySlug : string | null ,
111+ authenticationUrl : string | null
76112) : AuthCredentials | undefined {
77- const credentials = Cookie . get ( cookieName ( librarySlug ) ) ;
78- return credentials ? JSON . parse ( credentials ) : undefined ;
113+ if ( librarySlug === "ekirjasto" ) {
114+ // Get access token, for ekirjasto login credentials
115+ const accessToken = Cookie . get ( cookieNameEkirjasto ( ) ) ;
116+ // Create ekirjasto authentication credentials
117+ const authCredentials : AuthCredentials = {
118+ token : `Bearer ${ accessToken } ` ,
119+ methodType : OPDS1 . EkirjastoAuthType ,
120+ authenticationUrl : authenticationUrl ? authenticationUrl : undefined
121+ } ;
122+ // Return the credentials
123+ return authCredentials ? authCredentials : undefined ;
124+ } else {
125+ const credentials = Cookie . get ( cookieName ( librarySlug ) ) ;
126+ return credentials ? JSON . parse ( credentials ) : undefined ;
127+ }
79128}
80129
81130function setCredentialsCookie (
82131 librarySlug : string | null ,
83132 credentials : AuthCredentials
84133) {
85- Cookie . set ( cookieName ( librarySlug ) , JSON . stringify ( credentials ) ) ;
134+ if ( librarySlug === "ekirjasto" ) {
135+ Cookie . set ( cookieNameEkirjasto ( ) , JSON . stringify ( credentials ) ) ;
136+ } else {
137+ Cookie . set ( cookieName ( librarySlug ) , JSON . stringify ( credentials ) ) ;
138+ }
86139}
87140
88141function clearCredentialsCookie ( librarySlug : string | null ) {
89- Cookie . remove ( cookieName ( librarySlug ) ) ;
142+ if ( librarySlug === "ekirjasto" ) {
143+ Cookie . remove ( cookieNameEkirjasto ( ) ) ;
144+ } else {
145+ Cookie . remove ( cookieName ( librarySlug ) ) ;
146+ }
90147}
91148
92149export function generateToken ( username : string , password ?: string ) {
93150 return generateCredentials ( username , password ) ;
94151}
95152
96153/**
97- * URL CREDENTIALS
154+ * URL CREDENTIALS, NOT USED WITH EKIRJASTO
98155 */
99156function getUrlCredentials ( router : NextRouter ) {
100157 /* TODO: throw error if samlAccessToken and cleverAccessToken exist at the same time as this is an invalid state that shouldn't be reached */
0 commit comments