Skip to content

Commit

Permalink
Factor out api hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Mar 11, 2024
1 parent 2d06ffd commit f58270a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/services/authn.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const auth = new Authenticator<User>(sessionStorage)
auth.use(
new RfdApiStrategy(
{
host: process.env.RFD_API || '',
clientID: process.env.RFD_API_CLIENT_ID || '',
clientSecret: process.env.RFD_API_CLIENT_SECRET || '',
callbackURL: process.env.RFD_API_GOOGLE_CALLBACK_URL || '',
Expand All @@ -103,6 +104,7 @@ auth.use(
auth.use(
new RfdApiStrategy(
{
host: process.env.RFD_API || '',
clientID: process.env.RFD_API_CLIENT_ID || '',
clientSecret: process.env.RFD_API_CLIENT_SECRET || '',
callbackURL: process.env.RFD_API_GITHUB_CALLBACK_URL || '',
Expand Down
2 changes: 1 addition & 1 deletion app/services/rfdApi.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function apiRequest<T>(
headers['Authorization'] = `Bearer ${accessToken}`
}

const url = `https://rfd-api.shared.oxide.computer/${path.replace(/^\//, '')}`
const url = `${process.env.RFD_API}/${path.replace(/^\//, '')}`
console.info(`Requesting ${url} from the RFD API`)

const response = await fetch(url, { headers })
Expand Down
17 changes: 11 additions & 6 deletions app/utils/rfdApiStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import type { RfdApiPermission, RfdApiProvider, RfdScope } from './rfdApi'

export type RfdApiStrategyOptions = {
host: string
clientID: string
clientSecret: string
callbackURL: string
Expand Down Expand Up @@ -85,11 +86,10 @@ export class RfdApiStrategy<User extends ExpiringUser> extends OAuth2Strategy<
RfdApiExtraParams
> {
public name = `rfd-api`

private readonly userInfoURL = 'https://rfd-api.shared.oxide.computer/self'
protected host = ``

constructor(
{ clientID, clientSecret, callbackURL, remoteProvider, scope }: RfdApiStrategyOptions,
{ host, clientID, clientSecret, callbackURL, remoteProvider, scope }: RfdApiStrategyOptions,
verify: StrategyVerifyCallback<
User,
OAuth2StrategyVerifyParams<RfdApiProfile, RfdApiExtraParams>
Expand All @@ -100,13 +100,18 @@ export class RfdApiStrategy<User extends ExpiringUser> extends OAuth2Strategy<
clientID,
clientSecret,
callbackURL,
authorizationURL: `https://rfd-api.shared.oxide.computer/login/oauth/${remoteProvider}/code/authorize`,
tokenURL: `https://rfd-api.shared.oxide.computer/login/oauth/${remoteProvider}/code/token`,
authorizationURL: `${host}/login/oauth/${remoteProvider}/code/authorize`,
tokenURL: `${host}/login/oauth/${remoteProvider}/code/token`,
},
verify,
)
this.name = `${this.name}-${remoteProvider}`
this.scope = this.parseScope(scope)
this.host = host
}

protected userInfoUrl(): string {
return `${this.host}/self`
}

protected authorizationParams(): URLSearchParams {
Expand All @@ -115,7 +120,7 @@ export class RfdApiStrategy<User extends ExpiringUser> extends OAuth2Strategy<
}

protected async userProfile(accessToken: string): Promise<RfdApiProfile> {
const response = await fetch(this.userInfoURL, {
const response = await fetch(this.userInfoUrl(), {
headers: {
Authorization: `Bearer ${accessToken}`,
},
Expand Down

0 comments on commit f58270a

Please sign in to comment.