示例#1
0
文件: tokens.go 项目: pgpst/pgpst
func tokensAdd(c *cli.Context) int {
	// Connect to RethinkDB
	_, session, connected := connectToRethinkDB(c)
	if !connected {
		return 1
	}

	// Input struct
	var input struct {
		Owner      string    `json:"owner"`
		ExpiryDate time.Time `json:"expiry_date"`
		Type       string    `json:"type"`
		Scope      []string  `json:"scope"`
		ClientID   string    `json:"client_id"`
	}

	// Read JSON from stdin
	if c.Bool("json") {
		if err := json.NewDecoder(c.App.Env["reader"].(io.Reader)).Decode(&input); err != nil {
			writeError(c, err)
			return 1
		}
	} else {
		// Buffer stdin
		rd := bufio.NewReader(c.App.Env["reader"].(io.Reader))
		var err error

		// Acquire from interactive input
		fmt.Fprint(c.App.Writer, "Owner's ID: ")
		input.Owner, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Owner = strings.TrimSpace(input.Owner)

		fmt.Fprint(c.App.Writer, "Type [auth/activate/code]: ")
		input.Type, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Type = strings.TrimSpace(input.Type)

		fmt.Fprint(c.App.Writer, "Expiry date [2006-01-02T15:04:05Z07:00/empty]: ")
		expiryDate, err := rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		expiryDate = strings.TrimSpace(expiryDate)
		if expiryDate != "" {
			input.ExpiryDate, err = time.Parse(time.RFC3339, expiryDate)
			if err != nil {
				writeError(c, err)
				return 1
			}
		}

		if input.Type == "auth" || input.Type == "code" {
			fmt.Fprint(c.App.Writer, "Client ID: ")
			input.ClientID, err = rd.ReadString('\n')
			if err != nil {
				writeError(c, err)
				return 1
			}
			input.ClientID = strings.TrimSpace(input.ClientID)

			fmt.Fprint(c.App.Writer, "Scope (seperated by commas): ")
			scope, err := rd.ReadString('\n')
			if err != nil {
				writeError(c, err)
				return 1
			}
			scope = strings.TrimSpace(scope)
			input.Scope = strings.Split(scope, ",")
		}
	}

	// Validate the input

	// Type has to be either auth or activate
	if input.Type != "auth" && input.Type != "activate" && input.Type != "code" {
		writeError(c, fmt.Errorf("Token type must be either auth or activate. Got %s.", input.Type))
		return 1
	}

	// Scopes must exist
	if input.Scope != nil && len(input.Scope) > 0 {
		for _, scope := range input.Scope {
			if _, ok := models.Scopes[scope]; !ok {
				writeError(c, fmt.Errorf("Scope %s doesn't exist", scope))
				return 1
			}
		}
	}

	// Owner must exist
	cursor, err := r.Table("accounts").Get(input.Owner).Ne(nil).Run(session)
	if err != nil {
		writeError(c, err)
	}
	defer cursor.Close()
	var exists bool
	if err := cursor.One(&exists); err != nil {
		writeError(c, err)
		return 1
	}
	if !exists {
		writeError(c, fmt.Errorf("Account %s doesn't exist", input.Owner))
		return 1
	}

	// Application must exist
	if input.ClientID != "" {
		cursor, err = r.Table("applications").Get(input.ClientID).Ne(nil).Run(session)
		if err != nil {
			writeError(c, err)
		}
		defer cursor.Close()
		var exists bool
		if err := cursor.One(&exists); err != nil {
			writeError(c, err)
			return 1
		}
		if !exists {
			writeError(c, fmt.Errorf("Application %s doesn't exist", input.ClientID))
			return 1
		}
	}

	// Insert into database
	token := &models.Token{
		ID:           uniuri.NewLen(uniuri.UUIDLen),
		DateCreated:  time.Now(),
		DateModified: time.Now(),
		Owner:        input.Owner,
		ExpiryDate:   input.ExpiryDate,
		Type:         input.Type,
		Scope:        input.Scope,
		ClientID:     input.ClientID,
	}

	if !c.GlobalBool("dry") {
		if err := r.Table("tokens").Insert(token).Exec(session); err != nil {
			writeError(c, err)
			return 1
		}
	}

	// Write a success message
	fmt.Fprintf(c.App.Writer, "Created a new %s token with ID %s\n", token.Type, token.ID)
	return 0
}
示例#2
0
func applicationsAdd(c *cli.Context) int {
	// Connect to RethinkDB
	_, session, connected := connectToRethinkDB(c)
	if !connected {
		return 1
	}

	// Input struct
	var input struct {
		Owner       string `json:"owner"`
		Callback    string `json:"callback"`
		Homepage    string `json:"homepage"`
		Name        string `json:"name"`
		Description string `json:"description"`
	}

	// Read JSON from stdin
	if c.Bool("json") {
		if err := json.NewDecoder(c.App.Env["reader"].(io.Reader)).Decode(&input); err != nil {
			writeError(c, err)
			return 1
		}
	} else {
		// Buffer stdin
		rd := bufio.NewReader(c.App.Env["reader"].(io.Reader))
		var err error

		// Acquire from interactive input
		fmt.Fprint(c.App.Writer, "Owner's ID: ")
		input.Owner, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Owner = strings.TrimSpace(input.Owner)

		fmt.Fprint(c.App.Writer, "Application's name: ")
		input.Name, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Name = strings.TrimSpace(input.Name)

		fmt.Fprint(c.App.Writer, "Homepage URL: ")
		input.Homepage, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Homepage = strings.TrimSpace(input.Homepage)

		fmt.Fprint(c.App.Writer, "Description: ")
		input.Description, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Description = strings.TrimSpace(input.Description)

		fmt.Fprint(c.App.Writer, "Callback URL: ")
		input.Callback, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Callback = strings.TrimSpace(input.Callback)
	}

	// Validate the input

	// Check if account ID exists
	cursor, err := r.Table("accounts").Get(input.Owner).Ne(nil).Run(session)
	if err != nil {
		writeError(c, err)
		return 1
	}
	defer cursor.Close()
	var exists bool
	if err := cursor.One(&exists); err != nil {
		writeError(c, err)
		return 1
	}
	if !exists {
		writeError(c, fmt.Errorf("Account %s doesn't exist", input.Owner))
		return 1
	}

	// Homepage URL should be a URL
	if !govalidator.IsURL(input.Homepage) {
		writeError(c, fmt.Errorf("%s is not a URL", input.Homepage))
		return 1
	}

	// Callback URL should be a URL
	if !govalidator.IsURL(input.Callback) {
		writeError(c, fmt.Errorf("%s is not a URL", input.Callback))
		return 1
	}

	// Insert into database
	application := &models.Application{
		ID:           uniuri.NewLen(uniuri.UUIDLen),
		DateCreated:  time.Now(),
		DateModified: time.Now(),
		Owner:        input.Owner,
		Secret:       uniuri.NewLen(32),
		Callback:     input.Callback,
		Homepage:     input.Homepage,
		Name:         input.Name,
		Description:  input.Description,
	}

	if !c.GlobalBool("dry") {
		if err := r.Table("applications").Insert(application).Exec(session); err != nil {
			writeError(c, err)
			return 1
		}
	}

	// Write a success message
	fmt.Fprintf(c.App.Writer, "Created a new application with ID %s\n", application.ID)
	return 0
}
示例#3
0
文件: addresses.go 项目: pgpst/pgpst
func addressesAdd(c *cli.Context) int {
	// Connect to RethinkDB
	_, session, connected := connectToRethinkDB(c)
	if !connected {
		return 1
	}

	// Input struct
	var input struct {
		ID    string `json:"id"`
		Owner string `json:"owner"`
	}

	// Read JSON from stdin
	if c.Bool("json") {
		if err := json.NewDecoder(c.App.Env["reader"].(io.Reader)).Decode(&input); err != nil {
			writeError(c, err)
			return 1
		}
	} else {
		// Buffer stdin
		rd := bufio.NewReader(c.App.Env["reader"].(io.Reader))
		var err error

		// Acquire from interactive input
		fmt.Fprintf(c.App.Writer, "Address: ")
		input.ID, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.ID = strings.TrimSpace(input.ID)

		fmt.Fprintf(c.App.Writer, "Owner ID: ")
		input.Owner, err = rd.ReadString('\n')
		if err != nil {
			writeError(c, err)
			return 1
		}
		input.Owner = strings.TrimSpace(input.Owner)
	}

	// First of all, the address. Append domain if it has no such suffix.
	if strings.Index(input.ID, "@") == -1 {
		input.ID += "@" + c.GlobalString("default_domain")
	}

	// And format it
	styledID := utils.NormalizeAddress(input.ID)
	input.ID = utils.RemoveDots(styledID)

	// Then check if it's taken.
	cursor, err := r.Table("addresses").Get(input.ID).Ne(nil).Run(session)
	if err != nil {
		writeError(c, err)
		return 1
	}
	defer cursor.Close()
	var taken bool
	if err := cursor.One(&taken); err != nil {
		writeError(c, err)
		return 1
	}
	if taken {
		writeError(c, fmt.Errorf("Address %s is already taken", input.ID))
		return 1
	}

	// Check if account ID exists
	cursor, err = r.Table("accounts").Get(input.Owner).Ne(nil).Run(session)
	if err != nil {
		writeError(c, err)
	}
	defer cursor.Close()
	var exists bool
	if err := cursor.One(&exists); err != nil {
		writeError(c, err)
		return 1
	}
	if !exists {
		writeError(c, fmt.Errorf("Account %s doesn't exist", input.ID))
		return 1
	}

	// Insert the address into the database
	address := &models.Address{
		ID:           input.ID,
		StyledID:     styledID,
		DateCreated:  time.Now(),
		DateModified: time.Now(),
		Owner:        input.Owner,
	}

	if !c.GlobalBool("dry") {
		if err := r.Table("addresses").Insert(address).Exec(session); err != nil {
			writeError(c, err)
			return 1
		}
	}

	// Write a success message
	fmt.Fprintf(c.App.Writer, "Created a new address - %s\n", address.StyledID)
	return 0
}