-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Began migrating to cuny-blackboard-api instead of Blackboard directly
- Loading branch information
1 parent
d0b18e3
commit 1e580f9
Showing
8 changed files
with
293 additions
and
416 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
CLIENTS_JSON=/clients.json | ||
COMMAND_PREFIX=/blackboard | ||
DISCORD_APPLICATION_ID=Your Discord application ID here. Get this from the Discord Developer Portal | ||
DISCORD_BOT_TOKEN=Your Discord bot token here. Get this from the Discord Developer Portal | ||
DISCORD_BOT_TOKEN=Your Discord bot token here. Get this from the Discord Developer Portal | ||
BLACKBOARD_API_BASE=http://some-api-domain.com |
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,11 +1,8 @@ | ||
{ | ||
"type": "module", | ||
"dependencies": { | ||
"cheerio": "^1.0.0-rc.12", | ||
"discord.js": "^14.4.0", | ||
"dotenv": "^16.0.2", | ||
"fetch-cookie": "^2.1.0", | ||
"node-fetch": "^3.2.10", | ||
"when-time": "^1.0.3" | ||
} | ||
} |
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,50 +1,31 @@ | ||
import fetch from 'node-fetch'; | ||
import makeFetchCookie from 'fetch-cookie'; | ||
import { BLACKBOARD_URL_BASE, DEFAULT_USER_AGENT } from './client.js'; | ||
|
||
/** | ||
* Performs a Blackboard login to generate session cookies. | ||
* Performs a Blackboard login to generate session token. | ||
* | ||
* @param {String} username | ||
* @param {String} password | ||
* @returns {Promise<String>} The session cookies in header format | ||
* @returns {Promise<String>} The session token for Blackboard API | ||
*/ | ||
export async function perform_blackboard_login(username, password) { | ||
// Create a new fetch instance with cookie support | ||
const cookie_jar = new makeFetchCookie.toughCookie.CookieJar(); | ||
const fetch_with_cookies = makeFetchCookie(fetch, cookie_jar); | ||
|
||
// Fetch the Blackboard home page which will redirect to the login page | ||
const login_response = await fetch_with_cookies(BLACKBOARD_URL_BASE, { | ||
method: 'GET', | ||
headers: { | ||
'user-agent': DEFAULT_USER_AGENT, | ||
}, | ||
}); | ||
|
||
// Make a POST request to the login endpoint with the credentials | ||
const endpoint = `${new URL(login_response.url).origin}/oam/server/auth_cred_submit`; | ||
const endpoint_response = await fetch_with_cookies(endpoint, { | ||
// Make fetch request to generate a session token | ||
const response = await fetch(`${process.env['BLACKBOARD_API_BASE']}/login`, { | ||
method: 'POST', | ||
headers: { | ||
'user-agent': DEFAULT_USER_AGENT, | ||
'content-type': 'application/x-www-form-urlencoded', | ||
}, | ||
body: `usernameH=${encodeURIComponent(username)}&username=${encodeURIComponent( | ||
username.split('@')[0].toLowerCase() | ||
)}&password=${password}&submit=`, | ||
body: JSON.stringify({ | ||
username, | ||
password, | ||
}), | ||
}); | ||
|
||
// If we have arrived on the blackboard home page, we have successfully logged in | ||
if (endpoint_response.url.startsWith(BLACKBOARD_URL_BASE)) { | ||
// Return the session cookies in header format | ||
const cookies = await cookie_jar.getCookies(BLACKBOARD_URL_BASE, { | ||
allPaths: true, | ||
}); | ||
// Try to parse the response as JSON | ||
try { | ||
// Retrieve the token from the response | ||
const { token } = await response.json(); | ||
|
||
// Return the token if it is a valid string | ||
if (typeof token === 'string') return token; | ||
|
||
// Return the session cookies in header format | ||
return cookies.map((cookie) => `${cookie.key}=${cookie.value}`).join('; '); | ||
} else { | ||
// Otherwise, throw an error | ||
throw new Error('NO_TOKEN_RECEIVED'); | ||
} catch (error) { | ||
throw new Error('INVALID_CREDENTIALS'); | ||
} | ||
} |
Oops, something went wrong.