diff --git a/handlers/auth.go b/handlers/auth.go index 330029b..bc55add 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -17,9 +17,19 @@ import ( func (h *Handler) initOAuth() { h.engine.Use(sessions.Sessions("backend", sessions.NewCookieStore(util.GetPrivateKey()))) - auth.WithAdapterWrapper(auth.NewGoogleAdapter(viper.GetString("Google.ClientID"), viper.GetString("Google.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/google")) - auth.WithAdapterWrapper(auth.NewGithubAdapter(viper.GetString("GitHub.ClientID"), viper.GetString("GitHub.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/github")) - auth.WithAdapterWrapper(auth.NewMicrosoftAdapter(viper.GetString("Microsoft.ClientID"), viper.GetString("Microsoft.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/microsoft")) + h.providers = []string{} + if viper.GetString("Google.ClientSecret") != "" { + auth.WithAdapterWrapper(auth.NewGoogleAdapter(viper.GetString("Google.ClientID"), viper.GetString("Google.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/google")) + h.providers = append(h.providers, "google") + } + if viper.GetString("GitHub.ClientSecret") != "" { + auth.WithAdapterWrapper(auth.NewGithubAdapter(viper.GetString("GitHub.ClientID"), viper.GetString("GitHub.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/github")) + h.providers = append(h.providers, "github") + } + if viper.GetString("Microsoft.ClientSecret") != "" { + auth.WithAdapterWrapper(auth.NewMicrosoftAdapter(viper.GetString("Microsoft.ClientID"), viper.GetString("Microsoft.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/microsoft")) + h.providers = append(h.providers, "microsoft") + } h.engine.POST("/api/v1/check", h.handleAuthCheck) } diff --git a/handlers/handlers.go b/handlers/handlers.go index 0d826f5..a5b888a 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -20,8 +20,9 @@ import ( // Handler holds the funcs and attributes for the // http communication type Handler struct { - store store.Store - engine *gin.Engine + store store.Store + engine *gin.Engine + providers []string } // DoNotPrivateKeyChecking is used for testing @@ -71,6 +72,8 @@ func (h *Handler) setHandlers() error { protected.POST("/create", h.handleCreate) protected.POST("/lookup", h.handleLookup) + h.engine.GET("/api/v1/info", h.handleInfo) + h.engine.NoRoute(h.handleAccess, gin.WrapH(http.FileServer(FS(false)))) return nil } diff --git a/handlers/public.go b/handlers/public.go index 1bc381b..e700812 100644 --- a/handlers/public.go +++ b/handlers/public.go @@ -86,6 +86,10 @@ func (h *Handler) handleCreate(c *gin.Context) { c.JSON(http.StatusOK, data) } +func (h *Handler) handleInfo(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"providers": h.providers}) +} + func (h *Handler) getSchemaAndHost(c *gin.Context) string { protocol := "http" if c.Request.TLS != nil { diff --git a/static/src/index.js b/static/src/index.js index 9988787..97e03b9 100644 --- a/static/src/index.js +++ b/static/src/index.js @@ -14,7 +14,8 @@ export default class BaseComponent extends Component { open: true, userData: {}, authorized: false, - activeItem: "" + activeItem: "", + providers: [] } onOAuthClose() { @@ -25,6 +26,11 @@ export default class BaseComponent extends Component { componentWillMount() { this.checkAuth() + this.loadInfo() + } + + loadInfo = () => { + fetch('/api/v1/info').then(d => d.json()).then(d => this.setState({ providers: d.providers })) } checkAuth = () => { @@ -61,12 +67,17 @@ export default class BaseComponent extends Component { } onOAuthClick = provider => { window.addEventListener('message', this.onOAuthCallback, false); - // Open the oAuth window that is it centered in the middle of the screen - var wwidth = 400, - wHeight = 500; - var wLeft = (window.screen.width / 2) - (wwidth / 2); - var wTop = (window.screen.height / 2) - (wHeight / 2); - window.open(`/api/v1/auth/${provider}/login`, '', `width=${wwidth}, height=${wHeight}, top=${wTop}, left=${wLeft}`) + var url = `${window.location.origin}/api/v1/auth/${provider}/login`; + if (!this._oAuthPopup) { + // Open the oAuth window that is it centered in the middle of the screen + var wwidth = 400, + wHeight = 500; + var wLeft = (window.screen.width / 2) - (wwidth / 2); + var wTop = (window.screen.height / 2) - (wHeight / 2); + this._oAuthPopup = window.open(url, '', `width=${wwidth}, height=${wHeight}, top=${wTop}, left=${wLeft}`) + } else { + this._oAuthPopup.location = url; + } } handleLogout = () => { @@ -75,7 +86,7 @@ export default class BaseComponent extends Component { } render() { - const { open, authorized, activeItem, userData } = this.state + const { open, authorized, activeItem, userData, providers } = this.state if (!authorized) { return ( @@ -85,17 +96,24 @@ export default class BaseComponent extends Component {

The following authentication services are currently available:

- -
- -
- + {providers.length == 0 &&

There are currently no correct oAuth credential maintained.

} + {providers.indexOf("microsoft") != -1 &&
+ +
} + {providers.indexOf("microsoft") != -1 &&
+
+ +
} + {providers.indexOf("microsoft") != -1 &&
+
+ +
}