Example #1
0
func LoadClients(limit, offset int, sort map[string]int) (clients []*models.Client, err error) {
	if limit < 1 {
		limit = 1
	}
	if offset < 0 {
		offset = 0
	}

	var orders []string
	for k, v := range sort {
		if inArray(k, clients_sortable_fields) {
			var o string
			if v == ASCENDING {
				o = "ASC"
			} else {
				o = "DESC"
			}
			orders = append(orders, k+" "+o)
		}
	}

	str := `SELECT id, name, code, secret, redirect_uri, created
	  , allowed_grant_types, allowed_response_types, allowed_scopes
	   FROM oauth_client `

	if len(orders) > 0 {
		str = str + " ORDER BY " + strings.Join(orders, ",")
	}

	str = fmt.Sprintf("%s LIMIT %d OFFSET %d", str, limit, offset)

	clients = make([]*models.Client, 0)
	qs := func(db *sql.DB) error {
		rows, err := db.Query(str)
		if err != nil {
			log.Printf("db query error: %s for sql %s", err, str)
			return err
		}
		defer rows.Close()
		for rows.Next() {
			c := new(models.Client)
			var (
				grandTypes, responseTypes, scopes string
			)
			err = rows.Scan(&c.Id, &c.Name, &c.Code, &c.Secret, &c.RedirectUri, &c.CreatedAt,
				&grandTypes, &responseTypes, &scopes)
			if err != nil {
				log.Printf("rows scan error: %s", err)
				continue
			}
			c.AllowedGrantTypes = strings.Split(grandTypes, ",")
			c.AllowedResponseTypes = strings.Split(responseTypes, ",")
			c.AllowedScopes = strings.Split(scopes, ",")
			clients = append(clients, c)
		}
		return rows.Err()
	}

	if err := withDbQuery(qs); err != nil {
		return nil, err
	}

	return clients, nil
}
Example #2
0
func clientsPost(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
	if ctx.User == nil || ctx.User.IsExpired() || !ctx.User.IsKeeper() {
		http.Redirect(w, req, reverse("login"), http.StatusTemporaryRedirect)
		return nil
	}
	res := make(osin.ResponseData)

	var (
		client *models.Client
	)

	if req.FormValue("op") == "new" {
		// create new client
		client = models.NewClient(
			req.PostFormValue("name"),
			req.PostFormValue("code"),
			req.PostFormValue("secret"),
			req.PostFormValue("redirect_uri"))
		// log.Printf("new client: %v", client)
		_, e := backends.GetClientWithCode(client.Code) // check exists
		if e == nil {
			res["ok"] = false
			res["error"] = map[string]string{"message": "duplicate client_id"}
			return outputJson(res, w)
		}

	} else {

		pk, name, value := req.PostFormValue("pk"), req.PostFormValue("name"), req.PostFormValue("value")
		log.Printf("clientsPost: pk %s, name %s, value %s", pk, name, value)
		if pk == "" {
			res["ok"] = false
			res["error"] = map[string]string{"message": "pk is empty"}
			return outputJson(res, w)
		}
		// id, err := strconv.ParseUint(pk, 10, 32)
		client, err = backends.GetClientWithCode(pk)
		if err != nil {
			res["ok"] = false
			res["error"] = map[string]string{"message": "pk is invalid or not found"}
			return outputJson(res, w)
		}
		switch name {
		case "name":
			client.Name = value
		case "secret":
			client.Secret = value
		case "redirect_uri":
			client.RedirectUri = value
		default:
			log.Printf("invalid filed: %s", name)
		}
	}

	if client != nil {
		err = backends.SaveClient(client)
		if err != nil {
			res["ok"] = false
			res["error"] = map[string]string{"message": err.Error()}
			return outputJson(res, w)
		}
		res["ok"] = true
		res["id"] = client.Id
		return outputJson(res, w)
	}

	res["ok"] = false
	res["error"] = map[string]string{"message": "invalid operation"}
	return outputJson(res, w)
}