-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94d5333
commit e6d37f0
Showing
9 changed files
with
143 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
import { SelfHostedCheckPayload } from '@retrospected/common'; | ||
import config from '../config'; | ||
import fetch from 'node-fetch'; | ||
import wait from '../utils'; | ||
import wait, { comparePassword, decrypt } from '../utils'; | ||
import { LicenceMetadata } from 'src/types'; | ||
|
||
let licenced: boolean | null = null; | ||
let licenced: LicenceMetadata | null = null; | ||
|
||
type HardcodedLicence = { | ||
hash: string; | ||
encryptedOwner: string; | ||
}; | ||
|
||
const hardcodedLicences: HardcodedLicence[] = [ | ||
{ | ||
hash: '$2a$10$kt4DnxKZEwvoh052JFygru7iLiIrTzSJngcJlaYkWm.tlNzRJx/Di', | ||
encryptedOwner: 'U2FsdGVkX18/e8sfZ3bpjz3pLQkCxloH8nuniFdU+vo=', | ||
}, | ||
]; | ||
|
||
export function isSelfHostedAndLicenced() { | ||
return !!licenced && config.SELF_HOSTED; | ||
} | ||
|
||
export async function isLicenced() { | ||
export async function isLicenced(): Promise<LicenceMetadata | null> { | ||
if (licenced !== null) { | ||
return licenced; | ||
} | ||
|
@@ -19,15 +32,43 @@ export async function isLicenced() { | |
return result; | ||
} | ||
|
||
async function isLicencedBase() { | ||
async function checkHardcodedLicence( | ||
key: string | ||
): Promise<LicenceMetadata | null> { | ||
for (const hardcodedLicence of hardcodedLicences) { | ||
const match = await comparePassword(key, hardcodedLicence.hash); | ||
if (match) { | ||
const decrypted = decrypt(hardcodedLicence.encryptedOwner, key); | ||
return { | ||
licence: key, | ||
owner: decrypted, | ||
}; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
// async function buildHardcodedLicence( | ||
// licenceKey: string, | ||
// company: string | ||
// ): Promise<void> { | ||
// console.log('Building hardcoded licence for: ', licenceKey); | ||
// const hash = await hashPassword(licenceKey); | ||
// console.log('Hash: ', hash); | ||
// console.log('Encrypted company name: ', encrypt(company, licenceKey)); | ||
// } | ||
|
||
async function isLicencedBase(): Promise<LicenceMetadata | null> { | ||
if (process.env.NODE_ENV !== 'production') { | ||
return true; | ||
return { licence: 'dev', owner: 'dev' }; | ||
} | ||
|
||
const licenceKey = config.LICENCE_KEY; | ||
|
||
const payload: SelfHostedCheckPayload = { key: licenceKey }; | ||
try { | ||
const response = await fetch( | ||
'https://www.retrospected.com/api/self-hosted', | ||
'https://www.retrospected.com/api/self-hosted-licence', | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
|
@@ -37,16 +78,26 @@ async function isLicencedBase() { | |
} | ||
); | ||
if (response.ok) { | ||
const result = await response.text(); | ||
return result === 'true'; | ||
const result = (await response.json()) as LicenceMetadata; | ||
return result; | ||
} else { | ||
console.error('Could not contact the licence server'); | ||
console.error( | ||
'Could not contact the licence server. If you have a valid licence, please contact [email protected] for support.' | ||
); | ||
console.log(response.status, response.statusText); | ||
} | ||
} catch (err) { | ||
console.error('Could not contact the licence server'); | ||
console.error( | ||
'Could not contact the licence server. If you have a valid licence, please contact [email protected] for support.' | ||
); | ||
console.log(err); | ||
} | ||
|
||
return false; | ||
// Checking hardcoded licence as a last resort | ||
const hardcoded = await checkHardcodedLicence(licenceKey); | ||
if (hardcoded) { | ||
return hardcoded; | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters