Introducing new PKI management
This commit is contained in:
parent
5b558bcf00
commit
c118035c33
19 changed files with 857 additions and 53 deletions
|
@ -1,4 +1,105 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Certificate struct {
|
||||
Id int64 `json:"id,string"`
|
||||
Creation time.Time `json:"creation"`
|
||||
Password string `json:"password"`
|
||||
IdTeam *int64 `json:"id_team"`
|
||||
Revoked *time.Time `json:"revoked"`
|
||||
}
|
||||
|
||||
func GetCertificates() (certificates []Certificate, err error) {
|
||||
var rows *sql.Rows
|
||||
if rows, err = DBQuery("SELECT id_cert, creation, password, id_team, revoked FROM certificates ORDER BY creation"); err == nil {
|
||||
defer rows.Close()
|
||||
|
||||
certificates = make([]Certificate, 0)
|
||||
for rows.Next() {
|
||||
var c Certificate
|
||||
if err = rows.Scan(&c.Id, &c.Creation, &c.Password, &c.IdTeam, &c.Revoked); err != nil {
|
||||
return
|
||||
}
|
||||
certificates = append(certificates, c)
|
||||
}
|
||||
err = rows.Err()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetTeamCertificates(team Team) (certificates []Certificate, err error) {
|
||||
var rows *sql.Rows
|
||||
if rows, err = DBQuery("SELECT id_cert, creation, password, id_team, revoked FROM certificates WHERE id_team = ? ORDER BY creation", team.Id); err == nil {
|
||||
defer rows.Close()
|
||||
|
||||
certificates = make([]Certificate, 0)
|
||||
for rows.Next() {
|
||||
var c Certificate
|
||||
if err = rows.Scan(&c.Id, &c.Creation, &c.Password, &c.IdTeam, &c.Revoked); err != nil {
|
||||
return
|
||||
}
|
||||
certificates = append(certificates, c)
|
||||
}
|
||||
err = rows.Err()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetCertificate(serial int64) (c Certificate, err error) {
|
||||
err = DBQueryRow("SELECT id_cert, creation, password, id_team, revoked FROM certificates WHERE id_cert = ?", serial).Scan(&c.Id, &c.Creation, &c.Password, &c.IdTeam, &c.Revoked)
|
||||
return
|
||||
}
|
||||
|
||||
func ExistingCertSerial(serial int64) (bool) {
|
||||
c, _ := GetCertificate(serial)
|
||||
return c.Id > 0
|
||||
}
|
||||
|
||||
func RegisterCertificate(serial int64, password string) (Certificate, error) {
|
||||
now := time.Now()
|
||||
if _, err := DBExec("INSERT INTO certificates (id_cert, creation, password) VALUES (?, ?, ?)", serial, now, password); err != nil {
|
||||
return Certificate{}, err
|
||||
} else {
|
||||
return Certificate{serial, now, password, nil, nil}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c Certificate) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE certificates SET creation = ?, password = ?, id_team = ?, revoked = ? WHERE id_cert = ?", c.Creation, c.Password, c.IdTeam, c.Revoked, c.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Certificate) Revoke() (int64, error) {
|
||||
now := time.Now()
|
||||
c.Revoked = &now
|
||||
return c.Update()
|
||||
}
|
||||
|
||||
func (c Certificate) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM certificates WHERE id_cert = ?", c.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func ClearCertificates() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM certificates"); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue