-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
247 lines (223 loc) · 6.81 KB
/
server.js
File metadata and controls
247 lines (223 loc) · 6.81 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require('dotenv').config();
const { ApolloServer } = require('@apollo/server');
const { startStandaloneServer } = require('@apollo/server/standalone');
const { GraphQLClient } = require('graphql-request');
const { GraphQLError } = require('graphql');
const OktaJwtVerifier = require('@okta/jwt-verifier');
// JWT validation configuration
const OKTA_AUDIENCE = process.env.OKTA_AUDIENCE || 'backend';
const REQUIRED_SCOPES = (process.env.REQUIRED_SCOPES || 'backend-api:read').split(' ');
// Initialize Okta JWT Verifier
const oktaJwtVerifier = new OktaJwtVerifier({
issuer: process.env.OKTA_ISSUER,
clientId: OKTA_AUDIENCE,
assertClaims: {
// Issuer and expiration are verified by default
},
});
// Client to the external Countries API
const countriesClient = new GraphQLClient(
'https://countries.trevorblades.com/'
);
// Your schema
const typeDefs = `
type Country {
code: String!
name: String!
capital: String
currency: String
emoji: String!
continent: Continent!
}
type Continent {
code: String!
name: String!
}
type Query {
country(code: String!): Country
countries: [Country!]!
# Custom endpoints!
europeanCountries: [Country!]!
countriesByContinent(continentCode: String!): [Country!]!
}
`;
const resolvers = {
Query: {
// Simple proxy - forward the request
country: async (_, { code }) => {
const query = `
query GetCountry($code: ID!) {
country(code: $code) {
code
name
capital
currency
emoji
continent {
code
name
}
}
}
`;
const data = await countriesClient.request(query, { code });
return data.country;
},
countries: async () => {
const query = `
query {
countries {
code
name
capital
currency
emoji
continent {
code
name
}
}
}
`;
const data = await countriesClient.request(query);
return data.countries;
},
// Custom endpoint - filter for European countries
europeanCountries: async () => {
const query = `
query {
countries {
code
name
capital
currency
emoji
continent {
code
name
}
}
}
`;
const data = await countriesClient.request(query);
return data.countries.filter(
country => country.continent.code === 'EU'
);
},
// Custom endpoint with parameter
countriesByContinent: async (_, { continentCode }) => {
const query = `
query {
countries {
code
name
capital
currency
emoji
continent {
code
name
}
}
}
`;
const data = await countriesClient.request(query);
return data.countries.filter(
country => country.continent.code === continentCode
);
},
},
};
const server = new ApolloServer({ typeDefs, resolvers });
startStandaloneServer(server, {
context: async ({ req }) => {
const requireAuth = process.env.REQUIRE_AUTH === 'true';
// Extract the Bearer token from Authorization header
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
if (requireAuth) {
throw new GraphQLError('Authentication required. Please provide a Bearer token.', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}
console.log('Unauthenticated request (allowed)');
return {
user: null,
authenticated: false
};
}
const token = authHeader.replace('Bearer ', '');
try {
// Verify the JWT with Okta (validates audience, issuer, expiration)
const jwt = await oktaJwtVerifier.verifyAccessToken(token, OKTA_AUDIENCE);
// Validate required scopes
const tokenScopes = jwt.claims.scp
? (Array.isArray(jwt.claims.scp) ? jwt.claims.scp : jwt.claims.scp.split(' '))
: [];
const hasRequiredScopes = REQUIRED_SCOPES.every(scope =>
tokenScopes.includes(scope)
);
if (!hasRequiredScopes) {
console.error('Missing required scopes. Required:', REQUIRED_SCOPES, 'Got:', tokenScopes);
throw new GraphQLError('Insufficient permissions', {
extensions: {
code: 'FORBIDDEN',
http: { status: 403 },
required: REQUIRED_SCOPES,
provided: tokenScopes,
},
});
}
console.log('Authenticated request');
console.log('JWT Token Details:');
console.log(' Subject (sub):', jwt.claims.sub);
console.log(' Audience (aud):', jwt.claims.aud);
console.log(' Scopes (scp):', jwt.claims.scp || 'N/A');
console.log(' Issuer (iss):', jwt.claims.iss);
console.log(' Issued At:', new Date(jwt.claims.iat * 1000).toISOString());
console.log(' Expires At:', new Date(jwt.claims.exp * 1000).toISOString());
console.log(' Client ID (cid):', jwt.claims.cid || 'N/A');
console.log(' User ID (uid):', jwt.claims.uid || 'N/A');
// Log all other claims
const standardClaims = ['sub', 'aud', 'scp', 'iss', 'iat', 'exp', 'cid', 'uid'];
const customClaims = Object.keys(jwt.claims).filter(key => !standardClaims.includes(key));
if (customClaims.length > 0) {
console.log(' Custom Claims:', JSON.stringify(
Object.fromEntries(customClaims.map(key => [key, jwt.claims[key]])),
null,
2
));
}
// Return the verified JWT claims in context
return {
user: jwt.claims,
authenticated: true,
token
};
} catch (error) {
// If it's already a GraphQLError (e.g., from scope validation), rethrow it
if (error instanceof GraphQLError) {
throw error;
}
console.error('Token verification failed:', error.message);
throw new GraphQLError('Invalid or expired token', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}
},
listen: { port: parseInt(process.env.PORT) || 4000 },
}).then(({ url }) => {
const requireAuth = process.env.REQUIRE_AUTH === 'true';
console.log(`Proxy server ready at ${url}`);
console.log(`Proxying to Countries API`);
console.log(`Okta authentication: ${requireAuth ? 'REQUIRED' : 'OPTIONAL'}`);
console.log(` Issuer: ${process.env.OKTA_ISSUER}`);
console.log(` Required Audience: ${OKTA_AUDIENCE}`);
console.log(` Required Scopes: ${REQUIRED_SCOPES.join(' ')}`);
});