03ea72595d
* Initial Commit for OAuth API This builds and run and return the right error. Need to test it and then adding all users as possible client * Added mising dependency * just compile already... * Fixing template test * Imrpovements Moved db stuff in models Added some tests Added form in modpanel to add/update a client Added controllers for add/update of client * Added Forms + speed improvements Controller oauth client listing + html Controller oauth client delete + messages Messages on comment delete New ES config that disable ES if set to false. Improve load speed on local development Fix a load config bug Fix index admin & translation string sign_out broken by @ewhal * Sanitize empty strig in form array + css Multiple empty array of strings are sanitized for the oauth client create form Added some css for the form display * Upload and Create form works * Fix splitting response types * Removing required on secret when updating * fix travis error * Fix travis template test * Update dependency * Moved to jinzhu instead of azhao * randomizen secret on creation * Final touch on oath api improved display name fix grant form csrf fix login csrf on oauth * Fix gorm test * fix template test * Fixing deleted dependency issue * Make travis faster * Fix typo * Fix csrf for api calls * This shouldn't be exempt * Removing hard coded hash @ewhal Don't forget to replace the hash in tokens.go with another one * Added an example on how to use OAuth middleware * Renamed fosite utils to oauth2 utils
119 lignes
3,2 Kio
Go
119 lignes
3,2 Kio
Go
package gorm
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"reflect"
|
|
"regexp"
|
|
"strconv"
|
|
"time"
|
|
"unicode"
|
|
)
|
|
|
|
var (
|
|
defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
|
|
sqlRegexp = regexp.MustCompile(`\?`)
|
|
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
|
|
)
|
|
|
|
func isPrintable(s string) bool {
|
|
for _, r := range s {
|
|
if !unicode.IsPrint(r) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
|
|
if len(values) > 1 {
|
|
var (
|
|
sql string
|
|
formattedValues []string
|
|
level = values[0]
|
|
currentTime = "\n\033[33m[" + NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m"
|
|
source = fmt.Sprintf("\033[35m(%v)\033[0m", values[1])
|
|
)
|
|
|
|
messages = []interface{}{source, currentTime}
|
|
|
|
if level == "sql" {
|
|
// duration
|
|
messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))
|
|
// sql
|
|
|
|
for _, value := range values[4].([]interface{}) {
|
|
indirectValue := reflect.Indirect(reflect.ValueOf(value))
|
|
if indirectValue.IsValid() {
|
|
value = indirectValue.Interface()
|
|
if t, ok := value.(time.Time); ok {
|
|
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
|
|
} else if b, ok := value.([]byte); ok {
|
|
if str := string(b); isPrintable(str) {
|
|
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
|
|
} else {
|
|
formattedValues = append(formattedValues, "'<binary>'")
|
|
}
|
|
} else if r, ok := value.(driver.Valuer); ok {
|
|
if value, err := r.Value(); err == nil && value != nil {
|
|
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
|
|
} else {
|
|
formattedValues = append(formattedValues, "NULL")
|
|
}
|
|
} else {
|
|
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
|
|
}
|
|
} else {
|
|
formattedValues = append(formattedValues, "NULL")
|
|
}
|
|
}
|
|
|
|
// differentiate between $n placeholders or else treat like ?
|
|
if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
|
|
sql = values[3].(string)
|
|
for index, value := range formattedValues {
|
|
placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
|
|
sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
|
|
}
|
|
} else {
|
|
formattedValuesLength := len(formattedValues)
|
|
for index, value := range sqlRegexp.Split(values[3].(string), -1) {
|
|
sql += value
|
|
if index < formattedValuesLength {
|
|
sql += formattedValues[index]
|
|
}
|
|
}
|
|
}
|
|
|
|
messages = append(messages, sql)
|
|
messages = append(messages, fmt.Sprintf(" \n\033[36;31m[%v]\033[0m ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned "))
|
|
} else {
|
|
messages = append(messages, "\033[31;1m")
|
|
messages = append(messages, values[2:]...)
|
|
messages = append(messages, "\033[0m")
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
type logger interface {
|
|
Print(v ...interface{})
|
|
}
|
|
|
|
// LogWriter log writer interface
|
|
type LogWriter interface {
|
|
Println(v ...interface{})
|
|
}
|
|
|
|
// Logger default logger
|
|
type Logger struct {
|
|
LogWriter
|
|
}
|
|
|
|
// Print format & print log
|
|
func (logger Logger) Print(values ...interface{}) {
|
|
logger.Println(LogFormatter(values...)...)
|
|
}
|