1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-27 22:05:56 +00:00

Complete the OAuth/OAuth2 'high level' support

This commit is contained in:
Makis Maropoulos
2016-06-22 16:01:31 +03:00
parent 56f78567a2
commit 4a446ac1e2
8 changed files with 312 additions and 29 deletions

View File

@@ -6,9 +6,11 @@ import (
"sync"
"time"
"github.com/iris-contrib/gothic"
"github.com/kataras/iris/config"
"github.com/kataras/iris/logger"
"github.com/kataras/iris/websocket"
"github.com/markbates/goth"
"github.com/kataras/iris/mail"
"github.com/kataras/iris/render/rest"
@@ -68,10 +70,11 @@ const (
// Implements the FrameworkAPI
type Framework struct {
*muxAPI
rest *rest.Render
templates *template.Template
sessions *sessions.Manager
mailer mail.Service
rest *rest.Render
templates *template.Template
sessions *sessions.Manager
mailer mail.Service
oauthHandlers Middleware
// fields which are useful to the user/dev
HTTPServer *Server
Config *config.Iris
@@ -115,13 +118,48 @@ func (s *Framework) initialize() {
s.sessions = sessions.New(s.Config.Sessions)
}
//set the rest
// set the rest
s.rest = rest.New(s.Config.Render.Rest)
//set mail and templates if not already setted
// set mail and templates if not already setted
s.prepareMailer()
s.prepareTemplates()
// set the oauth providers from the OAuth configuration field
// the user still can set his/her own provider (using goth.UseProviders), if the configuration for the provider is not exists
// prepare the configs
s.Config.OAuth = config.DefaultOAuth().MergeSingle(s.Config.OAuth)
oauthProviders := s.Config.OAuth.GetAll(s.HTTPServer.FullHost())
if len(oauthProviders) > 0 {
goth.UseProviders(oauthProviders...)
// set the mux path to handle these providers
s.Get(s.Config.OAuth.Path+"/:provider", func(ctx *Context) {
err := gothic.BeginAuthHandler(ctx)
if err != nil {
s.Logger.Warningf("\n[IRIS: OAUTH] Error:" + err.Error())
}
})
authMiddleware := func(ctx *Context) {
user, err := gothic.CompleteUserAuth(ctx)
if err != nil {
ctx.EmitError(StatusUnauthorized)
ctx.Log(err.Error())
return
}
ctx.SetOAuthUser(user)
ctx.Next()
}
s.oauthHandlers = append([]Handler{HandlerFunc(authMiddleware)}, s.oauthHandlers...)
s.Handle(MethodGet, s.Config.OAuth.Path+"/:provider/callback", s.oauthHandlers...)("oauth")
}
// end of auth
// listen to websocket connections
websocket.RegisterServer(s, s.Websocket, s.Logger)