@@ -3,16 +3,13 @@ package api
33import (
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- }
0 commit comments