Browse Source

Aded show only the maintained oAuth providers in the frontend. Fix #41

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
0d7a5e49e0
  1. 10
      handlers/auth.go
  2. 3
      handlers/handlers.go
  3. 4
      handlers/public.go
  4. 24
      static/src/index.js

10
handlers/auth.go

@ -17,9 +17,19 @@ import (
func (h *Handler) initOAuth() {
h.engine.Use(sessions.Sessions("backend", sessions.NewCookieStore(util.GetPrivateKey())))
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)
}

3
handlers/handlers.go

@ -22,6 +22,7 @@ import (
type Handler struct {
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
}

4
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 {

24
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);
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);
window.open(`/api/v1/auth/${provider}/login`, '', `width=${wwidth}, height=${wHeight}, top=${wTop}, left=${wLeft}`)
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 (
<Modal size='tiny' open={open} onClose={this.onOAuthClose}>
@ -85,17 +96,24 @@ export default class BaseComponent extends Component {
<Modal.Content>
<p>The following authentication services are currently available:</p>
<div className='ui center aligned segment'>
{providers.length == 0 && <p>There are currently no correct oAuth credential maintained.</p>}
{providers.indexOf("microsoft") != -1 && <div>
<Button className='ui google plus button' onClick={this.onOAuthClick.bind(this, "google")}>
<Icon name='google' /> Login with Google
</Button>
</div>}
{providers.indexOf("microsoft") != -1 && <div>
<div className="ui divider"></div>
<Button style={{ backgroundColor: "#333", color: "white" }} onClick={this.onOAuthClick.bind(this, "github")}>
<Icon name='github' /> Login with GitHub
</Button>
</div>}
{providers.indexOf("microsoft") != -1 && <div>
<div className="ui divider"></div>
<Button style={{ backgroundColor: "#0067b8", color: "white" }} onClick={this.onOAuthClick.bind(this, "microsoft")}>
<Icon name='windows' /> Login with Microsoft
</Button>
</div>}
</div>
</Modal.Content>
</Modal>

Loading…
Cancel
Save