-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for multiple client certificates (#96)
- Loading branch information
Showing
15 changed files
with
386 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2024 Dima Krasner | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package front | ||
|
||
import "github.com/dimkr/tootik/front/text" | ||
|
||
func (h *Handler) approve(w text.Writer, r *Request, args ...string) { | ||
if r.User == nil { | ||
w.Redirect("/users") | ||
return | ||
} | ||
|
||
hash := args[1] | ||
|
||
r.Log.Info("Approving certificate", "user", r.User.PreferredUsername, "hash", hash) | ||
|
||
if res, err := h.DB.ExecContext( | ||
r.Context, | ||
` | ||
update certificates set approved = 1 | ||
where user = ? and hash = ? and approved = 0 | ||
`, | ||
r.User.PreferredUsername, | ||
hash, | ||
); err != nil { | ||
r.Log.Warn("Failed to approve certificate", "user", r.User.PreferredUsername, "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} else if n, err := res.RowsAffected(); err != nil { | ||
r.Log.Warn("Failed to approve certificate", "user", r.User.PreferredUsername, "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} else if n == 0 { | ||
r.Log.Warn("Certificate doesn't exist or already approved", "user", r.User.PreferredUsername, "hash", hash) | ||
w.Status(40, "Cannot approve certificate") | ||
return | ||
} | ||
|
||
w.Redirect("/users/certificates") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2024 Dima Krasner | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package front | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/dimkr/tootik/front/text" | ||
) | ||
|
||
func (h *Handler) certificates(w text.Writer, r *Request, args ...string) { | ||
if r.User == nil { | ||
w.Redirect("/users") | ||
return | ||
} | ||
|
||
rows, err := h.DB.QueryContext( | ||
r.Context, | ||
` | ||
select inserted, hash, approved, expires from certificates | ||
where user = ? | ||
order by inserted | ||
`, | ||
r.User.PreferredUsername, | ||
) | ||
if err != nil { | ||
r.Log.Warn("Failed to fetch certificates", "user", r.User.PreferredUsername, "error", err) | ||
w.Error() | ||
return | ||
} | ||
|
||
defer rows.Close() | ||
|
||
w.OK() | ||
w.Title("🎓 Certificates") | ||
|
||
first := true | ||
for rows.Next() { | ||
var inserted, expires int64 | ||
var hash string | ||
var approved int | ||
if err := rows.Scan(&inserted, &hash, &approved, &expires); err != nil { | ||
r.Log.Warn("Failed to fetch certificate", "user", r.User.PreferredUsername, "error", err) | ||
continue | ||
} | ||
|
||
if !first { | ||
w.Empty() | ||
} | ||
|
||
w.Item("SHA-256: " + hash) | ||
w.Item("Added: " + time.Unix(inserted, 0).Format(time.DateOnly)) | ||
w.Item("Expires: " + time.Unix(expires, 0).Format(time.DateOnly)) | ||
|
||
if approved == 0 { | ||
w.Link("/users/certificates/approve/"+hash, "🟢 Approve") | ||
w.Link("/users/certificates/revoke/"+hash, "🔴 Deny") | ||
} else { | ||
w.Link("/users/certificates/revoke/"+hash, "🔴 Revoke") | ||
} | ||
|
||
first = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2024 Dima Krasner | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package front | ||
|
||
import "github.com/dimkr/tootik/front/text" | ||
|
||
func (h *Handler) revoke(w text.Writer, r *Request, args ...string) { | ||
if r.User == nil { | ||
w.Redirect("/users") | ||
return | ||
} | ||
|
||
hash := args[1] | ||
|
||
r.Log.Info("Revoking certificate", "user", r.User.PreferredUsername, "hash", hash) | ||
|
||
if res, err := h.DB.ExecContext( | ||
r.Context, | ||
` | ||
delete from certificates | ||
where user = $1 and hash = $2 and exists (select 1 from certificates others where others.user = $1 and others.hash != $2 and others.approved = 1) | ||
`, | ||
r.User.PreferredUsername, | ||
hash, | ||
); err != nil { | ||
r.Log.Warn("Failed to revoke certificate", "user", r.User.PreferredUsername, "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} else if n, err := res.RowsAffected(); err != nil { | ||
r.Log.Warn("Failed to revoke certificate", "user", r.User.PreferredUsername, "hash", hash, "error", err) | ||
w.Error() | ||
return | ||
} else if n == 0 { | ||
r.Log.Warn("Certificate doesn't exist or already revoked", "user", r.User.PreferredUsername, "hash", hash) | ||
w.Status(40, "Cannot revoke certificate") | ||
return | ||
} | ||
|
||
w.Redirect("/users/certificates") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.