incalculable-hamburger-81333
11/06/2024, 8:47 AMbrief-egg-61061
11/06/2024, 8:48 AMbrief-egg-61061
11/06/2024, 8:48 AMgo import ory "<http://github.com/ory/client-go|github.com/ory/client-go>" var client = NewSDK() // Use this context to access Ory APIs which require an Ory API Key. var oryAuthedContext = context.WithValue(context.Background(), ory.ContextAccessToken, os.Getenv("ORY_API_KEY")) func NewSDK() *ory.APIClient { conf := ory.NewConfiguration() conf.Servers = ory.ServerConfigurations{{ URL: "https://{your-project}.<http://projects.oryapis.com/|projects.oryapis.com/>", }} return ory.NewAPIClient(conf) }
This approach ensures that your backend APIs are properly protected and only accessible with the appropriate authorization.incalculable-hamburger-81333
11/06/2024, 8:52 AMbrief-egg-61061
11/06/2024, 8:52 AMbrief-egg-61061
11/06/2024, 8:53 AMgo import ( "context" ory "<http://github.com/ory/keto-client-go|github.com/ory/keto-client-go>" ) func checkPermission(subject, action, object string) (bool, error) { client := ory.NewAPIClient(ory.NewConfiguration()) resp, _, err := client.PermissionApi.CheckPermission(context.Background()). Namespace("your-namespace"). Object(object). Relation(action). Subject(subject). Execute() if err != nil { return false, err } return resp.Allowed, nil }
This approach allows you to use Keto for permission checks without Oathkeeper, giving you more flexibility in your architecture. Remember to secure your Keto instance properly, as mentioned in the [Keto installation documentation](https://www.ory.sh/docs/keto/install): "The APIs of Ory open-source Servers don't come with integrated access control. This means that all requests sent to their APIs are considered authenticated, authorized, and will be executed. Leaving the APIs in this state can lead to severe security risks." While Oathkeeper is recommended for API access control, you can implement your own access control layer using the Keto SDK if you prefer not to use Oathkeeper.incalculable-hamburger-81333
11/06/2024, 8:55 AMincalculable-hamburger-81333
11/06/2024, 8:56 AMbrief-egg-61061
11/06/2024, 8:56 AMbrief-egg-61061
11/06/2024, 8:56 AMnpm install @ory/client
2. Then, you can set up the SDK client in your NodeJS application: javascript const { Configuration, FrontendApi, PermissionApi } = require("@ory/client") const ory = new FrontendApi( new Configuration({ basePath: "https://{YOUR-PROJECT-SLUG}.<http://projects.oryapis.com|projects.oryapis.com>", baseOptions: { withCredentials: true, }, }), ) const permissionApi = new PermissionApi( new Configuration({ basePath: "https://{YOUR-PROJECT-SLUG}.<http://projects.oryapis.com|projects.oryapis.com>", }), )
3. To check user identity (session), you can use the toSession
method: javascript const express = require("express") const app = express() app.get("/protected-route", async function (req, res) { try { const session = await ory.toSession({ cookie: req.header("cookie"), }) // User is authenticated, proceed with your logic res.json({ message: "Authenticated", user: session.data.identity }) } catch (error) { // User is not authenticated res.status(401).json({ message: "Unauthorized" }) } })
This example is based on the [Express.js integration guide](https://www.ory.sh/docs/getting-started/integrate-auth/expressjs#require-login-to-access-the-home-page). 4. To check permissions using Ory Permissions (Keto), you can use the checkPermission
method: javascript app.get("/resource/:id", async function (req, res) { try { const session = await ory.toSession({ cookie: req.header("cookie"), }) const permission = await permissionApi.checkPermission({ namespace: "your-namespace", object: req.params.id, relation: "read", subjectId: session.data.identity.id, }) if (permission.data.allowed) { // User has permission, proceed with your logic res.json({ message: "Access granted" }) } else { res.status(403).json({ message: "Access denied" }) } } catch (error) { res.status(401).json({ message: "Unauthorized" }) } })
This example combines session checking and permission checking, as described in the [Synchronous authorization flow guide](https://www.ory.sh/docs/keto/guides/simple-access-check-guide#synchronous-authorization-flow). Remember to replace {YOUR-PROJECT-SLUG}
with your actual Ory project slug, and adjust the namespace, object, and relation according to your permission model. By implementing these methods, you can protect your NodeJS backend APIs by checking both user identity and permissions using the Ory SDK.incalculable-hamburger-81333
11/06/2024, 9:09 AMbrief-egg-61061
11/06/2024, 9:09 AMbrief-egg-61061
11/06/2024, 9:10 AMincalculable-hamburger-81333
11/06/2024, 9:13 AMbrief-egg-61061
11/06/2024, 9:13 AMbrief-egg-61061
11/06/2024, 9:13 AM