Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use cas as middleware in gin? #27

Closed
jkgeekJack opened this issue Dec 23, 2018 · 2 comments
Closed

how to use cas as middleware in gin? #27

jkgeekJack opened this issue Dec 23, 2018 · 2 comments

Comments

@jkgeekJack
Copy link

No description provided.

@geoffgarside
Copy link
Contributor

I've not used Gin, but it should be pluggable as a normal HTTP middleware

@bbiao
Copy link
Contributor

bbiao commented May 9, 2019

You can do it like this @jkgeekJack

const casURL = ""

func Auth() gin.HandlerFunc {
	url, _ := url.Parse(casURL)
	client := cas.NewClient(&cas.Options{URL: url})
	handler := client.HandleFunc(func(writer http.ResponseWriter, request *http.Request) {
		// DO NOTHING
	})

	return func(ctx *gin.Context) {
                 // Call the normal method 
		handler.ServeHTTP(ctx.Writer, ctx.Request)

		if !cas.IsAuthenticated(ctx.Request) {
			// redirect to login url
                         ctx.Abort()
		}
		ctx.Next()
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants