Skip to content

Commit

Permalink
add go auth exmples and sdk code
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Oct 9, 2024
1 parent cd919be commit 3887837
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 13 deletions.
5 changes: 1 addition & 4 deletions sdk/assemblyscript/examples/auth/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Hypermode Simple Example Plugin

This is a _very_ simple example showing how to write a Hypermode plugin.
It doesn't do very much, but it should illustrate how to write a function.

See [./assembly/index.ts](./assembly/index.ts) for the implementation.
This is an example showing how to write a Hypermode plugin that uses auth.
2 changes: 1 addition & 1 deletion sdk/assemblyscript/examples/auth/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export class Claims {
public userId!: string;
}
export function getJWTClaims(): Claims {
return auth.jwt.getClaims<Claims>();
return auth.getJWTClaims<Claims>();
}
2 changes: 1 addition & 1 deletion sdk/assemblyscript/examples/auth/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-example",
"name": "auth-example",
"private": true,
"description": "Modus AssemblyScript Simple Example",
"author": "Hypermode Inc.",
Expand Down
8 changes: 5 additions & 3 deletions sdk/assemblyscript/src/assembly/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
*/
import { JSON } from "json-as";

export class jwt {
public static getClaims<T>(): T {
return JSON.parse<T>(process.env.get("JWT_CLAIMS"));
export function getJWTClaims<T>(): T {
const claims = process.env.get("JWT_CLAIMS");
if (!claims) {
throw new Error("JWT claims not found.");
}
return JSON.parse<T>(claims);
}
12 changes: 12 additions & 0 deletions sdk/go/examples/auth/build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@echo off

:: This build script works best for examples that are in this repository.
:: If you are using this as a template for your own project, you may need to modify this script,
:: to invoke the modus-go-build tool with the correct path to your project.

SET "PROJECTDIR=%~dp0"
pushd ..\..\tools\modus-go-build > nul
go run . "%PROJECTDIR%"
set "exit_code=%ERRORLEVEL%"
popd > nul
exit /b %exit_code%
12 changes: 12 additions & 0 deletions sdk/go/examples/auth/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# This build script works best for examples that are in this repository.
# If you are using this as a template for your own project, you may need to modify this script,
# to invoke the modus-go-build tool with the correct path to your project.

PROJECTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
pushd ../../tools/modus-go-build > /dev/null
go run . "$PROJECTDIR"
exit_code=$?
popd > /dev/null
exit $exit_code
7 changes: 7 additions & 0 deletions sdk/go/examples/auth/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module auth-example

go 1.23.0

require github.com/hypermodeinc/modus/sdk/go v0.0.0

replace github.com/hypermodeinc/modus/sdk/go => ../../
12 changes: 12 additions & 0 deletions sdk/go/examples/auth/hypermode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://manifest.hypermode.com/hypermode.json",
"models": {
// No models are used by this example, but if you add any, they would go here.
},
"hosts": {
// No hosts are used by this example, but if you add any, they would go here.
},
"collections": {
// No collections are used by this example, but if you add any, they would go here.
}
}
25 changes: 25 additions & 0 deletions sdk/go/examples/auth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This example is part of the Modus project, licensed under the Apache License 2.0.
* You may modify and use this example in accordance with the license.
* See the LICENSE file that accompanied this code for further details.
*/

package main

import (
"github.com/hypermodeinc/modus/sdk/go/pkg/auth"
)

type Claims struct {
Exp int64 `json:"exp"`
Iat int64 `json:"iat"`
Iss string `json:"iss"`
Jti string `json:"jti"`
Nbf int64 `json:"nbf"`
Sub string `json:"sub"`
UserId string `json:"user-id"`
}

func GetJWTClaims() (*Claims, error) {
return auth.GetJWTClaims[*Claims]()
}
4 changes: 0 additions & 4 deletions sdk/go/examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,3 @@ func TestLogging() {
//
// The console functions also allow you to better control the reported logging level.
}

func TestEnvVars() string {
return os.Getenv("JWT_CLAIMS")
}
24 changes: 24 additions & 0 deletions sdk/go/pkg/auth/jwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package auth

import (
"errors"
"os"

"github.com/hypermodeinc/modus/sdk/go/pkg/utils"
)

type JWT struct{}

func GetJWTClaims[T any]() (T, error) {
var claims T
claimsStr := os.Getenv("JWT_CLAIMS")
if claimsStr == "" {
return claims, errors.New("JWT claims not found")
}
err := utils.JsonDeserialize([]byte(claimsStr), &claims) // Convert claimsStr to byte slice
if err != nil {
return claims, err
}

return claims, nil
}

0 comments on commit 3887837

Please sign in to comment.