-
Notifications
You must be signed in to change notification settings - Fork 7
/
SolidAuthnLogic.ts
126 lines (112 loc) · 4.04 KB
/
SolidAuthnLogic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { namedNode, NamedNode, sym } from "rdflib";
import { appContext, offlineTestID } from "./authUtil";
import * as debug from '../util/debug'
import { Session } from "@inrupt/solid-client-authn-browser";
import { AuthenticationContext, AuthnLogic } from "../types";
export class SolidAuthnLogic implements AuthnLogic {
private session: Session;
constructor(solidAuthSession: Session) {
this.session = solidAuthSession;
}
// we created authSession getter because we want to access it as authn.authSession externally
get authSession():Session { return this.session }
currentUser(): NamedNode | null {
const app = appContext()
if (app.viewingNoAuthPage) {
return sym(app.webId)
}
if (this && this.session && this.session.info && this.session.info.webId && this.session.info.isLoggedIn) {
return sym(this.session.info.webId)
}
return offlineTestID() // null unless testing
}
/**
* Retrieves currently logged in webId from either
* defaultTestUser or SolidAuth
* Also activates a session after login
* @param [setUserCallback] Optional callback
* @returns Resolves with webId uri, if no callback provided
*/
async checkUser<T> (
setUserCallback?: (me: NamedNode | null) => T
): Promise<NamedNode | T | null> {
// Save hash for "restorePreviousSession"
const preLoginRedirectHash = new URL(window.location.href).hash
if (preLoginRedirectHash) {
window.localStorage.setItem('preLoginRedirectHash', preLoginRedirectHash)
}
this.session.onSessionRestore((url) => {
if (document.location.toString() !== url) history.replaceState(null, '', url)
})
/**
* Handle a successful authentication redirect
*/
const redirectUrl = new URL(window.location.href)
redirectUrl.hash = ''
await this.session
.handleIncomingRedirect({
restorePreviousSession: true,
url: redirectUrl.href
})
// Check to see if a hash was stored in local storage
const postLoginRedirectHash = window.localStorage.getItem('preLoginRedirectHash')
if (postLoginRedirectHash) {
const curUrl = new URL(window.location.href)
if (curUrl.hash !== postLoginRedirectHash) {
if (history.pushState) {
// debug.log('Setting window.location.has using pushState')
history.pushState(null, document.title, postLoginRedirectHash)
} else {
// debug.warn('Setting window.location.has using location.hash')
location.hash = postLoginRedirectHash
}
curUrl.hash = postLoginRedirectHash
}
// See https://stackoverflow.com/questions/3870057/how-can-i-update-window-location-hash-without-jumping-the-document
// window.location.href = curUrl.toString()// @@ See https://developer.mozilla.org/en-US/docs/Web/API/Window/location
window.localStorage.setItem('preLoginRedirectHash', '')
}
// Check to see if already logged in / have the WebID
let me = offlineTestID()
if (me) {
return Promise.resolve(setUserCallback ? setUserCallback(me) : me)
}
const webId = this.webIdFromSession(this.session.info)
if (webId) {
me = this.saveUser(webId)
}
if (me) {
debug.log(`(Logged in as ${me} by authentication)`)
}
return Promise.resolve(setUserCallback ? setUserCallback(me) : me)
}
/**
* Saves `webId` in `context.me`
* @param webId
* @param context
*
* @returns Returns the WebID, after setting it
*/
saveUser (
webId: NamedNode | string | null,
context?: AuthenticationContext
): NamedNode | null {
let webIdUri: string
if (webId) {
webIdUri = (typeof webId === 'string') ? webId : webId.uri
const me = namedNode(webIdUri)
if (context) {
context.me = me
}
return me
}
return null
}
/**
* @returns {Promise<string|null>} Resolves with WebID URI or null
*/
webIdFromSession (session?: { webId?: string, isLoggedIn: boolean }): string | null {
const webId = session?.webId && session.isLoggedIn ? session.webId : null
return webId
}
}