1- import { CodeChallengeMethod , OAuth2Client } from "../client.js" ;
1+ import { createS256CodeChallenge } from "../oauth2.js" ;
2+ import {
3+ createOAuth2Request ,
4+ encodeBasicCredentials ,
5+ joinURIAndPath ,
6+ sendTokenRequest
7+ } from "../request.js" ;
28
39import type { OAuth2Tokens } from "../oauth2.js" ;
4- import { joinURIAndPath } from "../request.js" ;
510
611export class MicrosoftEntraId {
712 private authorizationEndpoint : string ;
813 private tokenEndpoint : string ;
9-
10- private client : OAuth2Client ;
14+ private clientId : string ;
15+ private clientSecret : string | null ;
16+ private redirectURI : string ;
1117
1218 constructor ( tenant : string , clientId : string , clientSecret : string | null , redirectURI : string ) {
1319 this . authorizationEndpoint = joinURIAndPath (
@@ -20,34 +26,67 @@ export class MicrosoftEntraId {
2026 tenant ,
2127 "/oauth2/v2.0/token"
2228 ) ;
23- this . client = new OAuth2Client ( clientId , clientSecret , redirectURI ) ;
29+ this . clientId = clientId ;
30+ this . clientSecret = clientSecret ;
31+ this . redirectURI = redirectURI ;
2432 }
2533
2634 public createAuthorizationURL ( state : string , codeVerifier : string , scopes : string [ ] ) : URL {
27- const url = this . client . createAuthorizationURLWithPKCE (
28- this . authorizationEndpoint ,
29- state ,
30- CodeChallengeMethod . S256 ,
31- codeVerifier ,
32- scopes
33- ) ;
35+ const url = new URL ( this . authorizationEndpoint ) ;
36+ url . searchParams . set ( "response_type" , "code" ) ;
37+ url . searchParams . set ( "client_id" , this . clientId ) ;
38+ url . searchParams . set ( "redirect_uri" , this . redirectURI ) ;
39+ url . searchParams . set ( "state" , state ) ;
40+ const codeChallenge = createS256CodeChallenge ( codeVerifier ) ;
41+ url . searchParams . set ( "code_challenge_method" , "S256" ) ;
42+ url . searchParams . set ( "code_challenge" , codeChallenge ) ;
43+ if ( scopes . length > 0 ) {
44+ url . searchParams . set ( "scope" , scopes . join ( " " ) ) ;
45+ }
3446 return url ;
3547 }
3648
3749 public async validateAuthorizationCode (
3850 code : string ,
3951 codeVerifier : string
4052 ) : Promise < OAuth2Tokens > {
41- const tokens = await this . client . validateAuthorizationCode (
42- this . tokenEndpoint ,
43- code ,
44- codeVerifier
45- ) ;
53+ const body = new URLSearchParams ( ) ;
54+ body . set ( "grant_type" , "authorization_code" ) ;
55+ body . set ( "code" , code ) ;
56+ body . set ( "redirect_uri" , this . redirectURI ) ;
57+ body . set ( "code_verifier" , codeVerifier ) ;
58+ if ( this . clientSecret === null ) {
59+ body . set ( "client_id" , this . clientId ) ;
60+ }
61+ const request = createOAuth2Request ( this . tokenEndpoint , body ) ;
62+ // Origin header required for public clients. Value can be anything.
63+ request . headers . set ( "Origin" , "arctic" ) ;
64+ if ( this . clientSecret !== null ) {
65+ const encodedCredentials = encodeBasicCredentials ( this . clientId , this . clientId ) ;
66+ request . headers . set ( "Authorization" , `Basic ${ encodedCredentials } ` ) ;
67+ }
68+ const tokens = await sendTokenRequest ( request ) ;
4669 return tokens ;
4770 }
4871
4972 public async refreshAccessToken ( refreshToken : string , scopes : string [ ] ) : Promise < OAuth2Tokens > {
50- const tokens = await this . client . refreshAccessToken ( this . tokenEndpoint , refreshToken , scopes ) ;
73+ const body = new URLSearchParams ( ) ;
74+ body . set ( "grant_type" , "refresh_token" ) ;
75+ body . set ( "refresh_token" , refreshToken ) ;
76+ if ( this . clientSecret === null ) {
77+ body . set ( "client_id" , this . clientId ) ;
78+ }
79+ if ( scopes . length > 0 ) {
80+ body . set ( "scope" , scopes . join ( " " ) ) ;
81+ }
82+ const request = createOAuth2Request ( this . tokenEndpoint , body ) ;
83+ // Origin header required for public clients. Value can be anything.
84+ request . headers . set ( "Origin" , "arctic" ) ;
85+ if ( this . clientSecret !== null ) {
86+ const encodedCredentials = encodeBasicCredentials ( this . clientId , this . clientSecret ) ;
87+ request . headers . set ( "Authorization" , `Basic ${ encodedCredentials } ` ) ;
88+ }
89+ const tokens = await sendTokenRequest ( request ) ;
5190 return tokens ;
5291 }
5392}
0 commit comments