diff --git a/handlers/auth.go b/handlers/auth.go index 6e38db7..330029b 100644 --- a/handlers/auth.go +++ b/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) } diff --git a/handlers/auth/microsoft.go b/handlers/auth/microsoft.go new file mode 100644 index 0000000..538f4ee --- /dev/null +++ b/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 µsoftAdapter{&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" +} diff --git a/static/src/index.css b/static/src/index.css deleted file mode 100644 index 506b1da..0000000 --- a/static/src/index.css +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/static/src/index.js b/static/src/index.js index 2a84fea..9988787 100644 --- a/static/src/index.js +++ b/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 -

Currently you are only able to use Google as authentication service:

+

The following authentication services are currently available:

-
-