Skip to content

Commit

Permalink
Began migrating to cuny-blackboard-api instead of Blackboard directly
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikk221 committed Oct 27, 2022
1 parent d0b18e3 commit 1e580f9
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 416 deletions.
3 changes: 2 additions & 1 deletion example.env
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
3 changes: 0 additions & 3 deletions package.json
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"
}
}
55 changes: 18 additions & 37 deletions src/blackboard/auth.js
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');
}
}
Loading

0 comments on commit 1e580f9

Please sign in to comment.