-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.service.ts
More file actions
89 lines (74 loc) · 2.68 KB
/
Copy pathauth.service.ts
File metadata and controls
89 lines (74 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import axios from 'axios';
import qs from 'querystring';
import config from '../config/config';
import logger from '../utils/logger';
/**
* Service for handling SharePoint authentication
*/
class AuthService {
private accessToken: string | null = null;
private tokenExpiration: Date | null = null;
/**
* Get access token for Microsoft Graph API
* Uses client credentials flow for service-to-service authentication
*/
async getAccessToken(): Promise<string> {
try {
// Check if we have a valid token already
if (this.accessToken && this.tokenExpiration && this.tokenExpiration > new Date()) {
logger.debug('Using cached access token');
return this.accessToken;
}
const { tenantId, clientId, clientSecret } = config.sharepoint;
// Validate required configuration
if (!tenantId || !clientId || !clientSecret) {
throw new Error('Missing SharePoint authentication configuration');
}
logger.info('Requesting new access token from Microsoft Identity platform');
// Token endpoint for Microsoft Identity platform
const tokenEndpoint = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`;
// Request body for client credentials flow
const requestBody = {
client_id: clientId,
client_secret: clientSecret,
scope: 'https://graph.microsoft.com/.default',
grant_type: 'client_credentials',
};
// Make request to token endpoint
const response = await axios.post(
tokenEndpoint,
qs.stringify(requestBody),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
// Extract token and expiration
const { access_token, expires_in } = response.data;
// Calculate expiration date (subtract 5 minutes for safety margin)
const expirationDate = new Date();
expirationDate.setSeconds(expirationDate.getSeconds() + expires_in - 300);
// Store token and expiration
this.accessToken = access_token;
this.tokenExpiration = expirationDate;
logger.info('Successfully obtained access token', {
expiresAt: this.tokenExpiration.toISOString()
});
return access_token;
} catch (error: any) {
logger.error('Failed to get access token', {
error: error.message,
response: error.response?.data
});
throw new Error('SharePoint authentication failed');
}
}
/**
* Validate Templafy Bearer token
*/
validateBearerToken(token: string): boolean {
return token === config.auth.bearerToken;
}
}
export default new AuthService();