summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/markbates/goth/providers/azureadv2/session.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/markbates/goth/providers/azureadv2/session.go')
-rw-r--r--vendor/github.com/markbates/goth/providers/azureadv2/session.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/vendor/github.com/markbates/goth/providers/azureadv2/session.go b/vendor/github.com/markbates/goth/providers/azureadv2/session.go
new file mode 100644
index 0000000000..f2f0cd07cb
--- /dev/null
+++ b/vendor/github.com/markbates/goth/providers/azureadv2/session.go
@@ -0,0 +1,63 @@
+package azureadv2
+
+import (
+ "encoding/json"
+ "errors"
+ "strings"
+ "time"
+
+ "github.com/markbates/goth"
+)
+
+// Session is the implementation of `goth.Session`
+type Session struct {
+ AuthURL string `json:"au"`
+ AccessToken string `json:"at"`
+ RefreshToken string `json:"rt"`
+ ExpiresAt time.Time `json:"exp"`
+}
+
+// GetAuthURL will return the URL set by calling the `BeginAuth` func
+func (s Session) GetAuthURL() (string, error) {
+ if s.AuthURL == "" {
+ return "", errors.New(goth.NoAuthUrlErrorMessage)
+ }
+
+ return s.AuthURL, nil
+}
+
+// Authorize the session with AzureAD and return the access token to be stored for future use.
+func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) {
+ p := provider.(*Provider)
+ token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code"))
+ if err != nil {
+ return "", err
+ }
+
+ if !token.Valid() {
+ return "", errors.New("invalid token received from provider")
+ }
+
+ s.AccessToken = token.AccessToken
+ s.RefreshToken = token.RefreshToken
+ s.ExpiresAt = token.Expiry
+
+ return token.AccessToken, err
+}
+
+// Marshal the session into a string
+func (s Session) Marshal() string {
+ b, _ := json.Marshal(s)
+ return string(b)
+}
+
+func (s Session) String() string {
+ return s.Marshal()
+}
+
+// UnmarshalSession wil unmarshal a JSON string into a session.
+func (p *Provider) UnmarshalSession(data string) (goth.Session, error) {
+ session := &Session{}
+ err := json.NewDecoder(strings.NewReader(data)).Decode(session)
+ return session, err
+}