mirror of
https://github.com/documize/community.git
synced 2025-07-21 14:19:43 +02:00
User-based secrets for Gemini sections, Un/MarshallSecrets()
This commit is contained in:
parent
94c47eca93
commit
c3ef29ff2c
4 changed files with 261 additions and 173 deletions
|
@ -45,9 +45,27 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
||||||
|
|
||||||
this.set('config', config);
|
this.set('config', config);
|
||||||
|
|
||||||
if (this.get('config.userId') > 0) {
|
let self = this;
|
||||||
this.send('auth');
|
self.set('waiting', true);
|
||||||
|
this.get('sectionService').fetch(this.get('page'), "secrets", this.get('config'))
|
||||||
|
.then(function (response) {
|
||||||
|
console.log(response);
|
||||||
|
self.set('waiting', false);
|
||||||
|
|
||||||
|
self.set('config.APIKey', response.apikey);
|
||||||
|
self.set('config.url', response.url);
|
||||||
|
self.set('config.username', response.username);
|
||||||
|
|
||||||
|
if (response.apikey.length > 0 && response.url.length > 0 && response.username.length > 0) {
|
||||||
|
self.send('auth');
|
||||||
}
|
}
|
||||||
|
}, function (reason) { //jshint ignore: line
|
||||||
|
console.log(reason);
|
||||||
|
self.set('waiting', false);
|
||||||
|
if (self.get('config.userId') > 0) {
|
||||||
|
self.send('auth');
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
willDestroyElement() {
|
willDestroyElement() {
|
||||||
|
|
|
@ -74,12 +74,14 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
|
||||||
}
|
}
|
||||||
|
|
||||||
switch method {
|
switch method {
|
||||||
|
case "secrets":
|
||||||
|
secs(ctx, w, r)
|
||||||
case "auth":
|
case "auth":
|
||||||
auth(w, r)
|
auth(ctx, w, r)
|
||||||
case "workspace":
|
case "workspace":
|
||||||
workspace(w, r)
|
workspace(ctx, w, r)
|
||||||
case "items":
|
case "items":
|
||||||
items(w, r)
|
items(ctx, w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +95,7 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Clean()
|
c.Clean(ctx)
|
||||||
|
|
||||||
if len(c.URL) == 0 {
|
if len(c.URL) == 0 {
|
||||||
log.Info("Gemini.Refresh received empty URL")
|
log.Info("Gemini.Refresh received empty URL")
|
||||||
|
@ -150,7 +152,7 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func auth(w http.ResponseWriter, r *http.Request) {
|
func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
|
@ -167,7 +169,7 @@ func auth(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Clean()
|
config.Clean(nil) // don't look at the database for the parameters
|
||||||
|
|
||||||
if len(config.URL) == 0 {
|
if len(config.URL) == 0 {
|
||||||
provider.WriteMessage(w, "gemini", "Missing URL value")
|
provider.WriteMessage(w, "gemini", "Missing URL value")
|
||||||
|
@ -203,6 +205,8 @@ func auth(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.SaveSecrets(ctx)
|
||||||
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
var g = geminiUser{}
|
var g = geminiUser{}
|
||||||
|
|
||||||
|
@ -218,7 +222,7 @@ func auth(w http.ResponseWriter, r *http.Request) {
|
||||||
provider.WriteJSON(w, g)
|
provider.WriteJSON(w, g)
|
||||||
}
|
}
|
||||||
|
|
||||||
func workspace(w http.ResponseWriter, r *http.Request) {
|
func workspace(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
|
@ -235,7 +239,7 @@ func workspace(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Clean()
|
config.Clean(ctx)
|
||||||
|
|
||||||
if len(config.URL) == 0 {
|
if len(config.URL) == 0 {
|
||||||
provider.WriteMessage(w, "gemini", "Missing URL value")
|
provider.WriteMessage(w, "gemini", "Missing URL value")
|
||||||
|
@ -291,7 +295,7 @@ func workspace(w http.ResponseWriter, r *http.Request) {
|
||||||
provider.WriteJSON(w, workspace)
|
provider.WriteJSON(w, workspace)
|
||||||
}
|
}
|
||||||
|
|
||||||
func items(w http.ResponseWriter, r *http.Request) {
|
func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
|
@ -308,7 +312,7 @@ func items(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Clean()
|
config.Clean(ctx)
|
||||||
|
|
||||||
if len(config.URL) == 0 {
|
if len(config.URL) == 0 {
|
||||||
provider.WriteMessage(w, "gemini", "Missing URL value")
|
provider.WriteMessage(w, "gemini", "Missing URL value")
|
||||||
|
@ -367,3 +371,9 @@ func items(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
provider.WriteJSON(w, items)
|
provider.WriteJSON(w, items)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func secs(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
sec, err := getSecrets(ctx)
|
||||||
|
log.IfErr(err)
|
||||||
|
provider.WriteJSON(w, sec)
|
||||||
|
}
|
||||||
|
|
|
@ -11,7 +11,12 @@
|
||||||
|
|
||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/documize/community/documize/section/provider"
|
||||||
|
"github.com/documize/community/wordsmith/log"
|
||||||
|
)
|
||||||
|
|
||||||
// the HTML that is rendered by this section.
|
// the HTML that is rendered by this section.
|
||||||
const renderTemplate = `
|
const renderTemplate = `
|
||||||
|
@ -82,8 +87,37 @@ type geminiConfig struct {
|
||||||
Filter map[string]interface{} `json:"filter"`
|
Filter map[string]interface{} `json:"filter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *geminiConfig) Clean() {
|
func (c *geminiConfig) Clean(ctx *provider.Context) {
|
||||||
|
if ctx != nil {
|
||||||
|
sec, err := getSecrets(ctx)
|
||||||
|
if err == nil {
|
||||||
|
if len(sec.APIKey) > 0 && len(sec.Username) > 0 && len(sec.URL) > 0 {
|
||||||
|
c.APIKey = strings.TrimSpace(sec.APIKey)
|
||||||
|
c.Username = strings.TrimSpace(sec.Username)
|
||||||
|
c.URL = strings.TrimSpace(sec.URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
c.APIKey = strings.TrimSpace(c.APIKey)
|
c.APIKey = strings.TrimSpace(c.APIKey)
|
||||||
c.Username = strings.TrimSpace(c.Username)
|
c.Username = strings.TrimSpace(c.Username)
|
||||||
c.URL = strings.TrimSpace(c.URL)
|
c.URL = strings.TrimSpace(c.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *geminiConfig) SaveSecrets(ctx *provider.Context) {
|
||||||
|
var sec secrets
|
||||||
|
sec.APIKey = strings.TrimSpace(c.APIKey)
|
||||||
|
sec.Username = strings.TrimSpace(c.Username)
|
||||||
|
sec.URL = strings.TrimSpace(c.URL)
|
||||||
|
log.IfErr(ctx.MarshalSecrets(sec))
|
||||||
|
}
|
||||||
|
|
||||||
|
type secrets struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
APIKey string `json:"apikey"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSecrets(ctx *provider.Context) (sec secrets, err error) {
|
||||||
|
err = ctx.UnmarshalSecrets(&sec)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -210,7 +210,20 @@ func (c *Context) SaveSecrets(JSONobj string) error {
|
||||||
return errors.New("SaveSecrets() may only be called from within Command()")
|
return errors.New("SaveSecrets() may only be called from within Command()")
|
||||||
}
|
}
|
||||||
m := c.prov.Meta()
|
m := c.prov.Meta()
|
||||||
return request.UserConfigSetJSON(c.OrgID, c.UserID, m.ConfigHandle(), JSONobj)
|
return request.UserConfigSetJSON(c.OrgID, c.UserID, m.ContentType, JSONobj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalSecrets to the database.
|
||||||
|
// Parameter the same as for json.Marshal().
|
||||||
|
func (c *Context) MarshalSecrets(sec interface{}) error {
|
||||||
|
if !c.inCommand {
|
||||||
|
return errors.New("MarshalSecrets() may only be called from within Command()")
|
||||||
|
}
|
||||||
|
byts, err := json.Marshal(sec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.SaveSecrets(string(byts))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSecrets for the current context user/org.
|
// GetSecrets for the current context user/org.
|
||||||
|
@ -220,7 +233,20 @@ func (c *Context) SaveSecrets(JSONobj string) error {
|
||||||
// Errors return the empty string.
|
// Errors return the empty string.
|
||||||
func (c *Context) GetSecrets(JSONpath string) string {
|
func (c *Context) GetSecrets(JSONpath string) string {
|
||||||
m := c.prov.Meta()
|
m := c.prov.Meta()
|
||||||
return request.UserConfigGetJSON(c.OrgID, c.UserID, m.ConfigHandle(), JSONpath)
|
return request.UserConfigGetJSON(c.OrgID, c.UserID, m.ContentType, JSONpath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrNoSecrets is returned if no secret is found in the database.
|
||||||
|
var ErrNoSecrets = errors.New("no secrets in database")
|
||||||
|
|
||||||
|
// UnmarshalSecrets from the database.
|
||||||
|
// Parameter the same as for "v" in json.Unmarshal().
|
||||||
|
func (c *Context) UnmarshalSecrets(v interface{}) error {
|
||||||
|
secTxt := c.GetSecrets("") // get all the json of the secrets
|
||||||
|
if len(secTxt) > 0 {
|
||||||
|
return json.Unmarshal([]byte(secTxt), v)
|
||||||
|
}
|
||||||
|
return ErrNoSecrets
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort sections in order that that should be presented.
|
// sort sections in order that that should be presented.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue