-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1803 from AletheiaFact/1801-enhancement-support-m…
…2m-authentication-for-external-integrations Implement M2M authentication using ory hydra
- Loading branch information
Showing
17 changed files
with
307 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "ory_infra/hydra"] | ||
path = ory_infra/hydra | ||
url = https://github.com/ory/hydra.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
serve: | ||
cookies: | ||
same_site_mode: Lax | ||
|
||
urls: | ||
self: | ||
issuer: http://127.0.0.1:4444 | ||
consent: http://127.0.0.1:3000/consent | ||
login: http://127.0.0.1:3000/login | ||
logout: http://127.0.0.1:3000/logout | ||
|
||
secrets: | ||
system: | ||
- youReallyNeedToChangeThis | ||
|
||
oidc: | ||
subject_identifiers: | ||
supported_types: | ||
- pairwise | ||
- public | ||
pairwise: | ||
salt: youReallyNeedToChangeThis | ||
|
||
strategies: | ||
access_token: opaque | ||
|
||
ttl: | ||
access_token: 1h | ||
refresh_token: 720h | ||
|
||
oauth2: | ||
client_credentials: | ||
default_grant_allowed_scope: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common"; | ||
import { Reflector } from "@nestjs/core"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { Logger } from "@nestjs/common"; | ||
import { Configuration } from "@ory/client"; | ||
|
||
@Injectable() | ||
export abstract class BaseGuard implements CanActivate { | ||
protected logger = new Logger(BaseGuard.name); | ||
protected oryConfig = new Configuration({ | ||
basePath: this.configService.get<string>("ory.url"), | ||
accessToken: this.configService.get<string>("ory.access_token"), | ||
}); | ||
|
||
constructor( | ||
protected configService: ConfigService, | ||
protected readonly reflector: Reflector | ||
) {} | ||
|
||
// Implement this in child guards | ||
abstract canActivate(context: ExecutionContext): Promise<boolean> | boolean; | ||
|
||
/** | ||
* Utility to extract a bearer token from Authorization header. | ||
*/ | ||
protected extractBearerToken(authHeader?: string): string | null { | ||
if (!authHeader) return null; | ||
const matches = authHeader.match(/Bearer\s+(\S+)/); | ||
return matches?.[1] || null; | ||
} | ||
|
||
/** | ||
* You can implement a shared redirect or fallback logic here. | ||
*/ | ||
protected checkAndRedirect(request, response, isPublic, redirectPath) { | ||
const isAllowedPublicUrl = [ | ||
"/login", | ||
"/unauthorized", | ||
"/_next", | ||
"/api/.ory", | ||
"/api/health", | ||
"/sign-up", | ||
"/api/user/register", | ||
"/api/claim", // Allow this route to be public temporarily for testing | ||
].some((route) => request.url.startsWith(route)); | ||
|
||
const overridePublicRoutes = | ||
!isAllowedPublicUrl && | ||
this.configService.get<string>("override_public_routes"); | ||
|
||
if ( | ||
(isPublic && !overridePublicRoutes) || | ||
request.url.startsWith("/api") | ||
) { | ||
return true; | ||
} else { | ||
response.redirect(redirectPath); | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.