mirror of
https://github.com/documize/community.git
synced 2025-07-24 07:39:43 +02:00
removed old API
This commit is contained in:
parent
e284a46f86
commit
562872a4a8
105 changed files with 337 additions and 14496 deletions
|
@ -17,32 +17,33 @@ import (
|
|||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/core/api/request"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
gogithub "github.com/google/go-github/github"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func clientID() string {
|
||||
return request.ConfigString(meta.ConfigHandle(), "clientID")
|
||||
func clientID(ctx domain.RequestContext, s *domain.Store) string {
|
||||
return s.Setting.Get(meta.ConfigHandle(), "clientID")
|
||||
}
|
||||
|
||||
func clientSecret() string {
|
||||
return request.ConfigString(meta.ConfigHandle(), "clientSecret")
|
||||
func clientSecret(ctx domain.RequestContext, s *domain.Store) string {
|
||||
return s.Setting.Get(meta.ConfigHandle(), "clientSecret")
|
||||
}
|
||||
|
||||
func authorizationCallbackURL() string {
|
||||
func authorizationCallbackURL(ctx domain.RequestContext, s *domain.Store) string {
|
||||
// NOTE: URL value must have the path and query "/api/public/validate?section=github"
|
||||
return request.ConfigString(meta.ConfigHandle(), "authorizationCallbackURL")
|
||||
return s.Setting.Get(meta.ConfigHandle(), "authorizationCallbackURL")
|
||||
}
|
||||
|
||||
func validateToken(ptoken string) error {
|
||||
func validateToken(ctx provider.Context, s *domain.Store, ptoken string) error {
|
||||
// Github authorization check
|
||||
authClient := gogithub.NewClient((&gogithub.BasicAuthTransport{
|
||||
Username: clientID(),
|
||||
Password: clientSecret(),
|
||||
Username: clientID(ctx.Request, s),
|
||||
Password: clientSecret(ctx.Request, s),
|
||||
}).Client())
|
||||
_, _, err := authClient.Authorizations.Check(clientID(), ptoken)
|
||||
_, _, err := authClient.Authorizations.Check(clientID(ctx.Request, s), ptoken)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -56,14 +57,15 @@ func (*Provider) githubClient(config *githubConfig) *gogithub.Client {
|
|||
}
|
||||
|
||||
// Callback is called by a browser redirect from Github, via the validation endpoint
|
||||
func Callback(res http.ResponseWriter, req *http.Request) error {
|
||||
func Callback(rt *env.Runtime, s *domain.Store, res http.ResponseWriter, req *http.Request) error {
|
||||
ctx := domain.GetRequestContext(req)
|
||||
|
||||
code := req.URL.Query().Get("code")
|
||||
state := req.URL.Query().Get("state")
|
||||
|
||||
ghurl := "https://github.com/login/oauth/access_token"
|
||||
vals := "client_id=" + clientID()
|
||||
vals += "&client_secret=" + clientSecret()
|
||||
vals := "client_id=" + clientID(ctx, s)
|
||||
vals += "&client_secret=" + clientSecret(ctx, s)
|
||||
vals += "&code=" + code
|
||||
vals += "&state=" + state
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
gogithub "github.com/google/go-github/github"
|
||||
)
|
||||
|
@ -43,7 +44,8 @@ func init() {
|
|||
|
||||
// Provider represents GitHub
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
|
@ -67,8 +69,8 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
CID string `json:"clientID"`
|
||||
URL string `json:"authorizationCallbackURL"`
|
||||
}
|
||||
ret.CID = clientID()
|
||||
ret.URL = authorizationCallbackURL()
|
||||
ret.CID = clientID(ctx.Request, p.Store)
|
||||
ret.URL = authorizationCallbackURL(ctx.Request, p.Store)
|
||||
provider.WriteJSON(w, ret)
|
||||
return
|
||||
}
|
||||
|
@ -86,7 +88,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
if method == "saveSecret" { // secret Token update code
|
||||
|
||||
// write the new one, direct from JS
|
||||
if err = ctx.SaveSecrets(string(body)); err != nil {
|
||||
if err = ctx.SaveSecrets(string(body), p.Store); err != nil {
|
||||
p.Runtime.Log.Error("github settoken configuration", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
|
@ -108,7 +110,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
|
||||
config.Clean()
|
||||
// always use DB version of the token
|
||||
config.Token = ctx.GetSecrets("token") // get the secret token in the database
|
||||
config.Token = ctx.GetSecrets("token", p.Store) // get the secret token in the database
|
||||
|
||||
client := p.githubClient(&config)
|
||||
|
||||
|
@ -119,11 +121,11 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
if len(config.Token) == 0 {
|
||||
err = errors.New("empty github token")
|
||||
} else {
|
||||
err = validateToken(config.Token)
|
||||
err = validateToken(*ctx, p.Store, config.Token)
|
||||
}
|
||||
if err != nil {
|
||||
// token now invalid, so wipe it
|
||||
ctx.SaveSecrets("") // ignore error, already in an error state
|
||||
ctx.SaveSecrets("", p.Store) // ignore error, already in an error state
|
||||
p.Runtime.Log.Error("github check token validation", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
|
@ -156,7 +158,7 @@ func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) strin
|
|||
}
|
||||
|
||||
c.Clean()
|
||||
c.Token = ctx.GetSecrets("token")
|
||||
c.Token = ctx.GetSecrets("token", p.Store)
|
||||
|
||||
client := p.githubClient(&c)
|
||||
|
||||
|
@ -193,7 +195,7 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string {
|
|||
}
|
||||
|
||||
c.Clean()
|
||||
c.Token = ctx.GetSecrets("token")
|
||||
c.Token = ctx.GetSecrets("token", p.Store)
|
||||
|
||||
data = strings.TrimSpace(data)
|
||||
if len(data) == 0 {
|
||||
|
|
|
@ -20,7 +20,7 @@ import (
|
|||
gogithub "github.com/google/go-github/github"
|
||||
)
|
||||
|
||||
func listFailed(rt env.Runtime, method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) {
|
||||
func listFailed(rt *env.Runtime, method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) {
|
||||
switch method { // which list to choose?
|
||||
|
||||
case "owners":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue