mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
57 lines
1.3 KiB
8 years ago
|
package github
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/markbates/goth"
|
||
|
)
|
||
|
|
||
|
// Session stores data during the auth process with Github.
|
||
|
type Session struct {
|
||
|
AuthURL string
|
||
|
AccessToken string
|
||
|
}
|
||
|
|
||
|
// GetAuthURL will return the URL set by calling the `BeginAuth` function on the Github provider.
|
||
|
func (s Session) GetAuthURL() (string, error) {
|
||
|
if s.AuthURL == "" {
|
||
|
return "", errors.New(goth.NoAuthUrlErrorMessage)
|
||
|
}
|
||
|
return s.AuthURL, nil
|
||
|
}
|
||
|
|
||
|
// Authorize the session with Github 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
|
||
|
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
|
||
|
}
|