Skip to content

Commit def73e4

Browse files
mariokorteMario Korte
andauthored
Fixes and optimizations for server with oauth flow (#146)
* Expand OAuth scopes to include additional permissions for broader access. This fixes missing write access to resources. * Set `trust proxy` in Express and ensure correct protocol detection for OAuth endpoints. * Dynamically generate OAuth scopes based on endpoints for improved flexibility and adaptability. -> Support for org-mode added --------- Co-authored-by: Mario Korte <[email protected]>
1 parent 5e37ad3 commit def73e4

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/server.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import logger, { enableConsoleLogging } from './logger.js';
88
import { registerAuthTools } from './auth-tools.js';
99
import { registerGraphTools } from './graph-tools.js';
1010
import GraphClient from './graph-client.js';
11-
import AuthManager from './auth.js';
11+
import AuthManager, { buildScopesFromEndpoints } from './auth.js';
1212
import { MicrosoftOAuthProvider } from './oauth-provider.js';
1313
import {
1414
exchangeCodeForToken,
@@ -90,6 +90,7 @@ class MicrosoftGraphServer {
9090
const port = typeof this.options.http === 'string' ? parseInt(this.options.http) : 3000;
9191

9292
const app = express();
93+
app.set('trust proxy', true);
9394
app.use(express.json());
9495
app.use(express.urlencoded({ extended: true }));
9596

@@ -115,7 +116,11 @@ class MicrosoftGraphServer {
115116

116117
// OAuth Authorization Server Discovery
117118
app.get('/.well-known/oauth-authorization-server', async (req, res) => {
118-
const url = new URL(`${req.protocol}://${req.get('host')}`);
119+
const protocol = req.secure ? 'https' : 'http';
120+
const url = new URL(`${protocol}://${req.get('host')}`);
121+
122+
const scopes = buildScopesFromEndpoints(this.options.orgMode);
123+
119124
res.json({
120125
issuer: url.origin,
121126
authorization_endpoint: `${url.origin}/authorize`,
@@ -126,17 +131,21 @@ class MicrosoftGraphServer {
126131
grant_types_supported: ['authorization_code', 'refresh_token'],
127132
token_endpoint_auth_methods_supported: ['none'],
128133
code_challenge_methods_supported: ['S256'],
129-
scopes_supported: ['User.Read', 'Files.Read', 'Mail.Read'],
134+
scopes_supported: scopes,
130135
});
131136
});
132137

133138
// OAuth Protected Resource Discovery
134139
app.get('/.well-known/oauth-protected-resource', async (req, res) => {
135-
const url = new URL(`${req.protocol}://${req.get('host')}`);
140+
const protocol = req.secure ? 'https' : 'http';
141+
const url = new URL(`${protocol}://${req.get('host')}`);
142+
143+
const scopes = buildScopesFromEndpoints(this.options.orgMode);
144+
136145
res.json({
137146
resource: `${url.origin}/mcp`,
138147
authorization_servers: [url.origin],
139-
scopes_supported: ['User.Read', 'Files.Read', 'Mail.Read'],
148+
scopes_supported: scopes,
140149
bearer_methods_supported: ['header'],
141150
resource_documentation: `${url.origin}`,
142151
});

0 commit comments

Comments
 (0)