Browse Source

Added Microsoft oAuth support: Fix #23

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
3bcac436ba
  1. 1
      handlers/auth.go
  2. 64
      handlers/auth/microsoft.go
  3. 13
      static/src/index.css
  4. 10
      static/src/index.js

1
handlers/auth.go

@ -19,6 +19,7 @@ func (h *Handler) initOAuth() {
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.engine.POST("/api/v1/check", h.handleAuthCheck)
}

64
handlers/auth/microsoft.go

@ -0,0 +1,64 @@
package auth
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/oauth2/microsoft"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
type microsoftAdapter struct {
config *oauth2.Config
}
// NewMicrosoftAdapter creates an oAuth adapter out of the credentials and the baseURL
func NewMicrosoftAdapter(clientID, clientSecret, baseURL string) Adapter {
return &microsoftAdapter{&oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: baseURL + "/api/v1/auth/microsoft/callback",
Scopes: []string{
"wl.basic",
},
Endpoint: microsoft.LiveConnectEndpoint,
}}
}
func (a *microsoftAdapter) GetRedirectURL(state string) string {
return a.config.AuthCodeURL(state)
}
func (a *microsoftAdapter) GetUserData(state, code string) (*user, error) {
logrus.Debugf("Getting User Data with state: %s, and code: %s", state, code)
oAuthToken, err := a.config.Exchange(context.Background(), code)
if err != nil {
return nil, errors.Wrap(err, "could not exchange code")
}
oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get("https://apis.live.net/v5.0/me")
if err != nil {
return nil, errors.Wrap(err, "could not get user data")
}
defer oAuthUserInfoReq.Body.Close()
var mUser struct {
ID string `json:"id"`
Name string `json:"name"`
}
if err = json.NewDecoder(oAuthUserInfoReq.Body).Decode(&mUser); err != nil {
return nil, errors.Wrap(err, "decoding user info failed")
}
return &user{
ID: mUser.ID,
Name: mUser.Name,
Picture: fmt.Sprintf("https://apis.live.net/v5.0/%s/picture", mUser.ID),
}, nil
}
func (a *microsoftAdapter) GetOAuthProviderName() string {
return "microsoft"
}

13
static/src/index.css

@ -1,13 +0,0 @@
.swatch-github-gray-light {
background-color:#999;
}
.swatch-github-gray {
background-color:#767676;
}
.swatch-github-gray-dark {
background-color:#333;
}
.ui.github.button {
background-color: #333;
color: #fff;
}

10
static/src/index.js

@ -9,8 +9,6 @@ import Home from './Home/Home'
import ShareX from './ShareX/ShareX'
import Lookup from './Lookup/Lookup'
import './index.css'
export default class BaseComponent extends Component {
state = {
open: true,
@ -85,18 +83,18 @@ export default class BaseComponent extends Component {
Authentication
</Modal.Header>
<Modal.Content>
<p>Currently you are only able to use Google as authentication service:</p>
<p>The following authentication services are currently available:</p>
<div className='ui center aligned segment'>
<Button className='ui google plus button' onClick={this.onOAuthClick.bind(this, "google")}>
<Icon name='google' /> Login with Google
</Button>
<div className="ui divider"></div>
<Button className='github' onClick={this.onOAuthClick.bind(this, "github")}>
<Button style={{ backgroundColor: "#333", color: "white" }} onClick={this.onOAuthClick.bind(this, "github")}>
<Icon name='github' /> Login with GitHub
</Button>
<div className="ui divider"></div>
<Button color='twitter' onClick={this.onOAuthClick.bind(this, "twitter")}>
<Icon name='twitter' /> Login with Twitter
<Button style={{ backgroundColor: "#0067b8", color: "white" }} onClick={this.onOAuthClick.bind(this, "microsoft")}>
<Icon name='windows' /> Login with Microsoft
</Button>
</div>
</Modal.Content>

Loading…
Cancel
Save