starsz commented on code in PR #2608:
URL: https://github.com/apache/apisix-dashboard/pull/2608#discussion_r998991169


##########
api/internal/filter/oidc.go:
##########
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package filter
+
+import (
+       "net/http"
+
+       "github.com/coreos/go-oidc/v3/oidc"
+       "github.com/gin-gonic/gin"
+       "golang.org/x/oauth2"
+
+       "github.com/apisix/manager-api/internal/conf"
+       "github.com/apisix/manager-api/internal/log"
+)
+
+type Token struct {
+       AccessToken string
+}
+
+func (token *Token) Token() (*oauth2.Token, error) {
+       oauth2Token := &oauth2.Token{AccessToken: token.AccessToken}
+       return oauth2Token, nil
+}
+
+func Oidc() gin.HandlerFunc {
+       return func(c *gin.Context) {
+               if c.Request.URL.Path == "/apisix/admin/oidc/login" {
+                       url := conf.OidcConfig.AuthCodeURL(conf.State)
+                       c.Redirect(302, url)
+                       c.Abort()
+                       return
+               }
+
+               if c.Request.URL.Path == "/apisix/admin/oidc/callback" {
+                       state := c.Query("state")
+                       if state != conf.State {

Review Comment:
   Please add some log here.



##########
api/test/e2e/oidc/oidc_test.go:
##########
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package oidc_test
+
+import (
+       "bytes"
+       "context"
+       "io/ioutil"
+       "math/rand"
+       "net/http"
+       "net/url"
+       "strings"
+       "time"
+
+       "github.com/Nerzal/gocloak/v11"
+       "github.com/PuerkitoBio/goquery"
+       "github.com/onsi/ginkgo"
+       "github.com/onsi/gomega"
+
+       "github.com/apisix/manager-api/test/e2e/base"
+)
+
+var _ = ginkgo.Describe("Oidc-Login", func() {
+       ginkgo.Context("test apisix/admin/oidc/login", func() {
+               ginkgo.It("should return status-code 302", func() {
+                       
gomega.Expect(accessOidcLogin()).To(gomega.Equal(http.StatusFound))
+               })
+       })
+       ginkgo.Context("test apisix/admin/oidc/callback", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcCallback()).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessRoutesWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with invalid cookie", func() 
{
+               ginkgo.It("should return status-code 401", func() {
+                       
gomega.Expect(accessRoutesWithCookie(false)).To(gomega.Equal(http.StatusUnauthorized))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with invalid cookie", 
func() {
+               ginkgo.It("should return status-code 403", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(false)).To(gomega.Equal(http.StatusForbidden))
+               })
+       })
+})
+
+func AccessOidcLogin() http.Response {
+       var req *http.Request
+       var resp *http.Response
+       var Client = &http.Client{
+               CheckRedirect: func(req *http.Request, via []*http.Request) 
error {
+                       return http.ErrUseLastResponse
+               },
+       }
+
+       req, _ = http.NewRequest("GET", 
"http://127.0.0.1:9000/apisix/admin/oidc/login";, nil)
+       resp, _ = Client.Do(req)

Review Comment:
   Add check the error if `Client.Do`



##########
api/test/e2e/oidc/oidc_test.go:
##########
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package oidc_test
+
+import (
+       "bytes"
+       "context"
+       "io/ioutil"
+       "math/rand"
+       "net/http"
+       "net/url"
+       "strings"
+       "time"
+
+       "github.com/Nerzal/gocloak/v11"
+       "github.com/PuerkitoBio/goquery"
+       "github.com/onsi/ginkgo"
+       "github.com/onsi/gomega"
+
+       "github.com/apisix/manager-api/test/e2e/base"
+)
+
+var _ = ginkgo.Describe("Oidc-Login", func() {
+       ginkgo.Context("test apisix/admin/oidc/login", func() {
+               ginkgo.It("should return status-code 302", func() {
+                       
gomega.Expect(accessOidcLogin()).To(gomega.Equal(http.StatusFound))
+               })
+       })
+       ginkgo.Context("test apisix/admin/oidc/callback", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcCallback()).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessRoutesWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with invalid cookie", func() 
{
+               ginkgo.It("should return status-code 401", func() {
+                       
gomega.Expect(accessRoutesWithCookie(false)).To(gomega.Equal(http.StatusUnauthorized))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with invalid cookie", 
func() {
+               ginkgo.It("should return status-code 403", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(false)).To(gomega.Equal(http.StatusForbidden))
+               })
+       })
+})
+
+func AccessOidcLogin() http.Response {
+       var req *http.Request
+       var resp *http.Response
+       var Client = &http.Client{
+               CheckRedirect: func(req *http.Request, via []*http.Request) 
error {
+                       return http.ErrUseLastResponse
+               },
+       }
+
+       req, _ = http.NewRequest("GET", 
"http://127.0.0.1:9000/apisix/admin/oidc/login";, nil)
+       resp, _ = Client.Do(req)
+
+       // return status-code
+       return *resp
+}
+
+func accessOidcLogin() int {
+       // access apisix/admin/oidc/login
+       resp := AccessOidcLogin()
+       return resp.StatusCode
+}
+
+func createUser(authenticationUrl string) string {
+       u, _ := url.Parse(authenticationUrl)
+       client := gocloak.NewClient("http://"; + u.Host)
+       ctx := context.Background()
+       token, _ := client.LoginAdmin(ctx, "admin", "admin", "master")
+
+       username := GetRandomString(3)
+       user := gocloak.User{
+               FirstName: gocloak.StringP(GetRandomString(3)),
+               LastName:  gocloak.StringP(GetRandomString(3)),
+               Email:     gocloak.StringP(GetRandomString(3)),
+               Enabled:   gocloak.BoolP(true),
+               Username:  gocloak.StringP(username),
+       }
+
+       id, _ := client.CreateUser(ctx, token.AccessToken, "master", user)
+
+       _ = client.SetPassword(ctx, token.AccessToken, id, "master", "123456", 
false)
+       return username
+}
+
+func accessOidcCallback() int {
+       var authenticationUrl string
+       var loginUrl string
+       var req *http.Request
+       var resp *http.Response
+       var Client http.Client
+
+       // access apisix/admin/oidc/login to get the authentication-url
+       *resp = AccessOidcLogin()
+       authenticationUrl = resp.Header.Get("Location")
+
+       // create a user
+       username := createUser(authenticationUrl)
+
+       // access the authentication-url
+       req, _ = http.NewRequest("GET", authenticationUrl, nil)
+       resp, _ = Client.Do(req)

Review Comment:
   ```suggestion
        resp, err = Client.Do(req)
   ```
   
   gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "do request")
   



##########
api/test/e2e/oidc/oidc_test.go:
##########
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package oidc_test
+
+import (
+       "bytes"
+       "context"
+       "io/ioutil"
+       "math/rand"
+       "net/http"
+       "net/url"
+       "strings"
+       "time"
+
+       "github.com/Nerzal/gocloak/v11"
+       "github.com/PuerkitoBio/goquery"
+       "github.com/onsi/ginkgo"
+       "github.com/onsi/gomega"
+
+       "github.com/apisix/manager-api/test/e2e/base"
+)
+
+var _ = ginkgo.Describe("Oidc-Login", func() {
+       ginkgo.Context("test apisix/admin/oidc/login", func() {
+               ginkgo.It("should return status-code 302", func() {
+                       
gomega.Expect(accessOidcLogin()).To(gomega.Equal(http.StatusFound))
+               })
+       })
+       ginkgo.Context("test apisix/admin/oidc/callback", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcCallback()).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessRoutesWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with invalid cookie", func() 
{
+               ginkgo.It("should return status-code 401", func() {
+                       
gomega.Expect(accessRoutesWithCookie(false)).To(gomega.Equal(http.StatusUnauthorized))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with invalid cookie", 
func() {
+               ginkgo.It("should return status-code 403", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(false)).To(gomega.Equal(http.StatusForbidden))
+               })
+       })
+})
+
+func AccessOidcLogin() http.Response {
+       var req *http.Request
+       var resp *http.Response
+       var Client = &http.Client{

Review Comment:
   ```suggestion
        var client = &http.Client{
   ```



##########
api/test/e2e/oidc/oidc_test.go:
##########
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package oidc_test
+
+import (
+       "bytes"
+       "context"
+       "io/ioutil"
+       "math/rand"
+       "net/http"
+       "net/url"
+       "strings"
+       "time"
+
+       "github.com/Nerzal/gocloak/v11"
+       "github.com/PuerkitoBio/goquery"
+       "github.com/onsi/ginkgo"
+       "github.com/onsi/gomega"
+
+       "github.com/apisix/manager-api/test/e2e/base"
+)
+
+var _ = ginkgo.Describe("Oidc-Login", func() {
+       ginkgo.Context("test apisix/admin/oidc/login", func() {
+               ginkgo.It("should return status-code 302", func() {
+                       
gomega.Expect(accessOidcLogin()).To(gomega.Equal(http.StatusFound))
+               })
+       })
+       ginkgo.Context("test apisix/admin/oidc/callback", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcCallback()).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessRoutesWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with cookie", func() {
+               ginkgo.It("should return status-code 200", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(true)).To(gomega.Equal(http.StatusOK))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/routes with invalid cookie", func() 
{
+               ginkgo.It("should return status-code 401", func() {
+                       
gomega.Expect(accessRoutesWithCookie(false)).To(gomega.Equal(http.StatusUnauthorized))
+               })
+       })
+
+       ginkgo.Context("access apisix/admin/oidc/logout with invalid cookie", 
func() {
+               ginkgo.It("should return status-code 403", func() {
+                       
gomega.Expect(accessOidcLogoutWithCookie(false)).To(gomega.Equal(http.StatusForbidden))
+               })
+       })
+})
+
+func AccessOidcLogin() http.Response {
+       var req *http.Request
+       var resp *http.Response
+       var Client = &http.Client{
+               CheckRedirect: func(req *http.Request, via []*http.Request) 
error {
+                       return http.ErrUseLastResponse
+               },
+       }
+
+       req, _ = http.NewRequest("GET", 
"http://127.0.0.1:9000/apisix/admin/oidc/login";, nil)
+       resp, _ = Client.Do(req)

Review Comment:
   ```suggestion
        resp, _ = client.Do(req)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to