@@ -4,8 +4,10 @@ import (
44 "net/http"
55 "strings"
66
7+ "github.com/authorizerdev/authorizer/server/constants"
78 "github.com/authorizerdev/authorizer/server/cookie"
89 "github.com/authorizerdev/authorizer/server/db"
10+ "github.com/authorizerdev/authorizer/server/envstore"
911 "github.com/authorizerdev/authorizer/server/sessionstore"
1012 "github.com/authorizerdev/authorizer/server/token"
1113 "github.com/gin-gonic/gin"
@@ -27,12 +29,38 @@ func AuthorizeHandler() gin.HandlerFunc {
2729 state := strings .TrimSpace (gc .Query ("state" ))
2830 codeChallenge := strings .TrimSpace (gc .Query ("code_challenge" ))
2931 scopeString := strings .TrimSpace (gc .Query ("scope" ))
30- scope := [] string {}
32+ clientID := strings . TrimSpace ( gc . Query ( "client_id" ))
3133 template := "authorize.tmpl"
3234
35+ if clientID == "" {
36+ gc .HTML (http .StatusOK , template , gin.H {
37+ "target_origin" : redirectURI ,
38+ "authorization_response" : map [string ]interface {}{
39+ "type" : "authorization_response" ,
40+ "response" : map [string ]string {
41+ "error" : "client_id is required" ,
42+ },
43+ },
44+ })
45+ return
46+ }
47+
48+ if clientID != envstore .EnvStoreObj .GetStringStoreEnvVariable (constants .EnvKeyClientID ) {
49+ gc .HTML (http .StatusOK , template , gin.H {
50+ "target_origin" : redirectURI ,
51+ "authorization_response" : map [string ]interface {}{
52+ "type" : "authorization_response" ,
53+ "response" : map [string ]string {
54+ "error" : "invalid_client_id" ,
55+ },
56+ },
57+ })
58+ return
59+ }
60+
3361 if redirectURI == "" {
3462 gc .HTML (http .StatusOK , template , gin.H {
35- "target_origin" : nil ,
63+ "target_origin" : redirectURI ,
3664 "authorization_response" : map [string ]interface {}{
3765 "type" : "authorization_response" ,
3866 "response" : map [string ]string {
@@ -45,7 +73,7 @@ func AuthorizeHandler() gin.HandlerFunc {
4573
4674 if state == "" {
4775 gc .HTML (http .StatusOK , template , gin.H {
48- "target_origin" : nil ,
76+ "target_origin" : redirectURI ,
4977 "authorization_response" : map [string ]interface {}{
5078 "type" : "authorization_response" ,
5179 "response" : map [string ]string {
@@ -60,16 +88,19 @@ func AuthorizeHandler() gin.HandlerFunc {
6088 responseType = "token"
6189 }
6290
91+ var scope []string
6392 if scopeString == "" {
6493 scope = []string {"openid" , "profile" , "email" }
94+ } else {
95+ scope = strings .Split (scopeString , " " )
6596 }
6697
6798 isResponseTypeCode := responseType == "code"
6899 isResponseTypeToken := responseType == "token"
69100
70101 if ! isResponseTypeCode && ! isResponseTypeToken {
71102 gc .HTML (http .StatusOK , template , gin.H {
72- "target_origin" : nil ,
103+ "target_origin" : redirectURI ,
73104 "authorization_response" : map [string ]interface {}{
74105 "type" : "authorization_response" ,
75106 "response" : map [string ]string {
@@ -83,7 +114,7 @@ func AuthorizeHandler() gin.HandlerFunc {
83114 if isResponseTypeCode {
84115 if codeChallenge == "" {
85116 gc .HTML (http .StatusBadRequest , template , gin.H {
86- "target_origin" : nil ,
117+ "target_origin" : redirectURI ,
87118 "authorization_response" : map [string ]interface {}{
88119 "type" : "authorization_response" ,
89120 "response" : map [string ]string {
@@ -98,7 +129,7 @@ func AuthorizeHandler() gin.HandlerFunc {
98129 sessionToken , err := cookie .GetSession (gc )
99130 if err != nil {
100131 gc .HTML (http .StatusOK , template , gin.H {
101- "target_origin" : nil ,
132+ "target_origin" : redirectURI ,
102133 "authorization_response" : map [string ]interface {}{
103134 "type" : "authorization_response" ,
104135 "response" : map [string ]string {
@@ -114,7 +145,7 @@ func AuthorizeHandler() gin.HandlerFunc {
114145 claims , err := token .ValidateBrowserSession (gc , sessionToken )
115146 if err != nil {
116147 gc .HTML (http .StatusOK , template , gin.H {
117- "target_origin" : nil ,
148+ "target_origin" : redirectURI ,
118149 "authorization_response" : map [string ]interface {}{
119150 "type" : "authorization_response" ,
120151 "response" : map [string ]string {
@@ -129,7 +160,7 @@ func AuthorizeHandler() gin.HandlerFunc {
129160 user , err := db .Provider .GetUserByID (userID )
130161 if err != nil {
131162 gc .HTML (http .StatusOK , template , gin.H {
132- "target_origin" : nil ,
163+ "target_origin" : redirectURI ,
133164 "authorization_response" : map [string ]interface {}{
134165 "type" : "authorization_response" ,
135166 "response" : map [string ]string {
@@ -150,7 +181,7 @@ func AuthorizeHandler() gin.HandlerFunc {
150181 newSessionTokenData , newSessionToken , err := token .CreateSessionToken (user , nonce , claims .Roles , scope )
151182 if err != nil {
152183 gc .HTML (http .StatusOK , template , gin.H {
153- "target_origin" : nil ,
184+ "target_origin" : redirectURI ,
154185 "authorization_response" : map [string ]interface {}{
155186 "type" : "authorization_response" ,
156187 "response" : map [string ]string {
@@ -168,9 +199,12 @@ func AuthorizeHandler() gin.HandlerFunc {
168199 sessionstore .SetState (codeChallenge , code + "@" + newSessionToken )
169200 gc .HTML (http .StatusOK , template , gin.H {
170201 "target_origin" : redirectURI ,
171- "authorization_response" : map [string ]string {
172- "code" : code ,
173- "state" : state ,
202+ "authorization_response" : map [string ]interface {}{
203+ "type" : "authorization_response" ,
204+ "response" : map [string ]string {
205+ "code" : code ,
206+ "state" : state ,
207+ },
174208 },
175209 })
176210 return
@@ -181,7 +215,7 @@ func AuthorizeHandler() gin.HandlerFunc {
181215 authToken , err := token .CreateAuthToken (gc , user , claims .Roles , scope )
182216 if err != nil {
183217 gc .HTML (http .StatusOK , template , gin.H {
184- "target_origin" : nil ,
218+ "target_origin" : redirectURI ,
185219 "authorization_response" : map [string ]interface {}{
186220 "type" : "authorization_response" ,
187221 "response" : map [string ]string {
@@ -213,15 +247,18 @@ func AuthorizeHandler() gin.HandlerFunc {
213247 }
214248
215249 gc .HTML (http .StatusOK , template , gin.H {
216- "target_origin" : redirectURI ,
217- "authorization_response" : res ,
250+ "target_origin" : redirectURI ,
251+ "authorization_response" : map [string ]interface {}{
252+ "type" : "authorization_response" ,
253+ "response" : res ,
254+ },
218255 })
219256 return
220257 }
221258
222259 // by default return with error
223260 gc .HTML (http .StatusOK , template , gin.H {
224- "target_origin" : nil ,
261+ "target_origin" : redirectURI ,
225262 "authorization_response" : map [string ]interface {}{
226263 "type" : "authorization_response" ,
227264 "response" : map [string ]string {
0 commit comments