Skip to content

Commit 11be074

Browse files
Merge pull request #48 from sdslabs/application-routes-upgrade
Upgrades docker changes
2 parents d0d1649 + 5517df0 commit 11be074

File tree

10 files changed

+261
-98
lines changed

10 files changed

+261
-98
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ target/
3434

3535
# Exclude sql files
3636
*.sql
37+
38+
config.docker.yaml

Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
FROM golang:1.23-alpine
22

3-
WORKDIR /usr/app
3+
WORKDIR /app
44

5-
COPY . /usr/app/
5+
COPY . /app/
66

77
RUN export GOPROXY=direct
88

9-
RUN go build -o nymeria ./cmd/nymeria/main.go
10-
119
EXPOSE 9898
1210

11+
# install make, psql
12+
RUN apk add --no-cache make postgresql-client
13+
RUN make build
14+
1315
CMD ["./nymeria"]

api/application.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ func HandleCreateApplication(c *gin.Context) {
4646
return
4747
}
4848

49-
err = db.CreateApplication(body.Name, body.RedirectURL, body.AllowedDomains, body.Organization, helper.RandomString(10), helper.RandomString(30))
49+
clientKey := helper.RandomString(10)
50+
clientSecret := helper.RandomString(30)
51+
52+
err = db.CreateApplication(body.Name, body.RedirectURL, body.AllowedDomains, body.Organization, clientKey, clientSecret)
5053

5154
if err != nil {
5255
log.ErrorLogger("Create application failed", err)
@@ -62,6 +65,14 @@ func HandleCreateApplication(c *gin.Context) {
6265

6366
c.JSON(http.StatusOK, gin.H{
6467
"message": "application created",
68+
"application_credentials": gin.H{
69+
"name": body.Name,
70+
"client_key": clientKey,
71+
"client_secret": clientSecret,
72+
"redirect_url": body.RedirectURL,
73+
"allowed_domains": body.AllowedDomains,
74+
"organization": body.Organization,
75+
},
6576
})
6677

6778
}

api/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"net/http"
45
"time"
56

67
"github.com/gin-contrib/cors"
@@ -60,7 +61,11 @@ func Start() {
6061
r.GET("/verify-session", HandleVerifySession)
6162

6263
// Application Authorization
63-
r.POST("/verify-app", HandleAppAuthorization)
64+
r.POST("/verify-app", middleware.HandleAppAuthorization, func(c *gin.Context) {
65+
c.JSON(http.StatusOK, gin.H{
66+
"message": "Authorized",
67+
})
68+
})
6469

6570
// Admin Routes
6671
r.Use(middleware.OnlyAdmin)

api/status.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ package api
33
import (
44
"context"
55
"net/http"
6-
"strconv"
7-
"strings"
86

97
"github.com/gin-gonic/gin"
108
client "github.com/ory/client-go"
119

1210
"github.com/sdslabs/nymeria/config"
1311
"github.com/sdslabs/nymeria/helper"
1412
"github.com/sdslabs/nymeria/log"
15-
"github.com/sdslabs/nymeria/pkg/db"
1613
)
1714

1815
// HandleVerifySession handles the user session verification request
@@ -52,72 +49,3 @@ func HandleVerifySession(c *gin.Context) {
5249
"message": "Session verified",
5350
})
5451
}
55-
56-
// HandleAppAuthorization handles the application authorization request
57-
func HandleAppAuthorization(c *gin.Context) {
58-
var body SecureAccessProfileRequest
59-
err := c.BindJSON(&body)
60-
if err != nil {
61-
log.ErrorLogger("Unable to process json body", err)
62-
errCode := helper.ExtractErrorCode(err)
63-
c.JSON(errCode, gin.H{
64-
"error": strings.Split(err.Error(), " ")[1],
65-
"message": "Unable to process json body",
66-
})
67-
return
68-
}
69-
70-
// Get the application using only the client key
71-
app, err := db.GetApplicationByKey(body.ClientKey)
72-
if err != nil {
73-
log.ErrorLogger("Unable to get application", err)
74-
errCode := helper.ExtractErrorCode(err)
75-
c.JSON(errCode, gin.H{
76-
"error": strings.Split(err.Error(), " ")[1],
77-
"message": "Internal Server Error",
78-
})
79-
c.Abort()
80-
return
81-
}
82-
83-
timestampInt, err := strconv.ParseInt(body.Timestamp, 10, 64)
84-
if err != nil {
85-
log.ErrorLogger("Invalid timestamp", err)
86-
c.JSON(400, gin.H{
87-
"error": "bad_request",
88-
"message": "Invalid timestamp",
89-
})
90-
}
91-
92-
// Validate the signature - this proves the client has the secret without sending it
93-
isValid := helper.ValidateSignature(
94-
body.ClientKey,
95-
app.ClientSecret,
96-
body.RedirectURL,
97-
body.Signature,
98-
timestampInt,
99-
)
100-
101-
if !isValid {
102-
log.ErrorLogger("Invalid signature or expired request", nil)
103-
c.JSON(401, gin.H{
104-
"error": "unauthorized",
105-
"message": "Invalid signature or expired request",
106-
})
107-
return
108-
}
109-
110-
// Check if redirect URL matches
111-
if app.RedirectURL != body.RedirectURL {
112-
log.ErrorLogger("Redirect URL does not match", nil)
113-
c.JSON(400, gin.H{
114-
"error": "bad_request",
115-
"message": "Redirect URL does not match",
116-
})
117-
return
118-
}
119-
120-
c.JSON(http.StatusOK, gin.H{
121-
"message": "Authorized",
122-
})
123-
}

api/types.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package api
22

3-
import "time"
4-
53
type ApplicationPostBody struct {
64
Name string `json:"name"`
75
RedirectURL string `json:"redirect_url"`
@@ -24,21 +22,3 @@ type ApplicationBody struct {
2422
type IdentityBody struct {
2523
Identity string `json:"identity"`
2624
}
27-
28-
type VerifiableIdentityAddress struct {
29-
CreatedAt *time.Time `json:"created_at,omitempty"`
30-
Id *string `json:"id,omitempty"`
31-
Status string `json:"status"`
32-
UpdatedAt *time.Time `json:"updated_at,omitempty"`
33-
Value string `json:"value"`
34-
Verified bool `json:"verified"`
35-
VerifiedAt *time.Time `json:"verified_at,omitempty"`
36-
Via string `json:"via"`
37-
}
38-
39-
type SecureAccessProfileRequest struct {
40-
RedirectURL string `json:"redirect_url"`
41-
ClientKey string `json:"client_key"`
42-
Timestamp string `json:"timestamp"` // Unix timestamp to prevent replay attacks
43-
Signature string `json:"signature"` // HMAC signature of "client_key:timestamp:redirect_url"
44-
}

docker-compose.dev.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
services:
2+
db:
3+
image: postgres:latest
4+
environment:
5+
POSTGRES_USER: "kratos"
6+
POSTGRES_PASSWORD: "secret"
7+
POSTGRES_DB: "kratos"
8+
volumes:
9+
- db-data:/var/lib/postgresql/data
10+
networks:
11+
- nymeria-network
12+
app:
13+
container_name: nymeria-app
14+
build: .
15+
ports:
16+
- "9898:9898"
17+
depends_on:
18+
- db
19+
- kratos
20+
networks:
21+
- nymeria-network
22+
kratos-migrate:
23+
image: oryd/kratos:v1.3.1
24+
environment:
25+
- DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true&mode=rwc
26+
volumes:
27+
- type: volume
28+
source: kratos-sqlite
29+
target: /var/lib/sqlite
30+
read_only: false
31+
- type: bind
32+
source: ./config
33+
target: /etc/config/kratos
34+
command: -c /etc/config/kratos/kratos.yml migrate sql -e --yes
35+
restart: on-failure
36+
networks:
37+
- nymeria-network
38+
kratos:
39+
depends_on:
40+
- kratos-migrate
41+
image: oryd/kratos:v1.3.1
42+
restart: unless-stopped
43+
environment:
44+
- DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true
45+
- LOG_LEVEL=trace
46+
command: serve -c /etc/config/kratos/kratos.yml --dev --watch-courier
47+
ports:
48+
- "4433:4433"
49+
- "4434:4434"
50+
volumes:
51+
- type: volume
52+
source: kratos-sqlite
53+
target: /var/lib/sqlite
54+
read_only: false
55+
- type: bind
56+
source: ./config
57+
target: /etc/config/kratos
58+
networks:
59+
- nymeria-network
60+
mailslurper:
61+
image: oryd/mailslurper:latest-smtps
62+
ports:
63+
- "4436:4436"
64+
- "4437:4437"
65+
networks:
66+
- nymeria-network
67+
68+
volumes:
69+
db-data:
70+
name: nymeria-db
71+
kratos-sqlite:
72+
73+
networks:
74+
nymeria-network:
75+
name: nymeria-network

docker-compose.prod.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
services:
2+
db:
3+
image: postgres:latest
4+
environment:
5+
POSTGRES_USER: "kratos"
6+
POSTGRES_PASSWORD: "secret"
7+
POSTGRES_DB: "kratos"
8+
volumes:
9+
- db-data:/var/lib/postgresql/data
10+
networks:
11+
- nymeria-network
12+
app:
13+
container_name: nymeria-app
14+
build: .
15+
ports:
16+
- "9898:9898"
17+
depends_on:
18+
- db
19+
- kratos
20+
networks:
21+
- nymeria-network
22+
kratos-migrate:
23+
image: oryd/kratos:v1.3.1
24+
environment:
25+
- DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true&mode=rwc
26+
volumes:
27+
- type: volume
28+
source: kratos-sqlite
29+
target: /var/lib/sqlite
30+
read_only: false
31+
- type: bind
32+
source: ./config
33+
target: /etc/config/kratos
34+
command: -c /etc/config/kratos/kratos.yml migrate sql -e --yes
35+
restart: on-failure
36+
networks:
37+
- nymeria-network
38+
kratos:
39+
depends_on:
40+
- kratos-migrate
41+
image: oryd/kratos:v1.3.1
42+
restart: unless-stopped
43+
environment:
44+
- DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true
45+
- LOG_LEVEL=trace
46+
command: serve -c /etc/config/kratos/kratos.yml --dev --watch-courier
47+
ports:
48+
- "4433:4433"
49+
- "4434:4434"
50+
volumes:
51+
- type: volume
52+
source: kratos-sqlite
53+
target: /var/lib/sqlite
54+
read_only: false
55+
- type: bind
56+
source: ./config
57+
target: /etc/config/kratos
58+
networks:
59+
- nymeria-network
60+
61+
volumes:
62+
db-data:
63+
name: nymeria-db
64+
kratos-sqlite:
65+
66+
networks:
67+
nymeria-network:
68+
name: nymeria-network

0 commit comments

Comments
 (0)