mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-16 02:09:18 +08:00
950f2e2074
* add google+ * sort signin oauth2 providers based on the name so order is always the same * update auth tip for google+ * add gitlab provider * add bitbucket provider (and some go fmt) * add twitter provider * add facebook provider * add dropbox provider * add openid connect provider incl. new format of tips section in "Add New Source" * lower the amount of disk storage for each session to prevent issues while building cross platform (and disk overflow) * imports according to goimport and code style * make it possible to set custom urls to gitlab and github provider (only these could have a different host) * split up oauth2 into multiple files * small typo in comment * fix indention * fix indentation * fix new line before external import * fix layout of signin part * update "broken" dependency
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package facebook
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/markbates/goth"
|
|
)
|
|
|
|
// Session stores data during the auth process with Facebook.
|
|
type Session struct {
|
|
AuthURL string
|
|
AccessToken string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
// GetAuthURL will return the URL set by calling the `BeginAuth` function on the Facebook provider.
|
|
func (s Session) GetAuthURL() (string, error) {
|
|
if s.AuthURL == "" {
|
|
return "", errors.New(goth.NoAuthUrlErrorMessage)
|
|
}
|
|
return s.AuthURL, nil
|
|
}
|
|
|
|
// Authorize the session with Facebook 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.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 will unmarshal a JSON string into a session.
|
|
func (p *Provider) UnmarshalSession(data string) (goth.Session, error) {
|
|
sess := &Session{}
|
|
err := json.NewDecoder(strings.NewReader(data)).Decode(sess)
|
|
return sess, err
|
|
}
|