Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TJTS5901 K23 Template
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Startuplab
Courses
TJTS5901 Continuous Software Engineering
TJTS5901 K23 Template
Commits
2088e144
Commit
2088e144
authored
2 years ago
by
Teemu Autto
Browse files
Options
Downloads
Patches
Plain Diff
Page for access token creation.
parent
f7129c76
No related branches found
Branches containing commit
No related tags found
1 merge request
!70
Tokens
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/tjts5901/auth.py
+40
-2
40 additions, 2 deletions
src/tjts5901/auth.py
src/tjts5901/templates/auth/tokens.html
+61
-0
61 additions, 0 deletions
src/tjts5901/templates/auth/tokens.html
with
101 additions
and
2 deletions
src/tjts5901/auth.py
+
40
−
2
View file @
2088e144
from
datetime
import
datetime
import
functools
import
logging
...
...
@@ -11,11 +12,11 @@ from flask_login import (
login_required
,
current_user
,
)
from
flask_babel
import
_
from
werkzeug.security
import
check_password_hash
,
generate_password_hash
from
sentry_sdk
import
set_user
from
.models
import
User
,
Item
from
.models
import
AccessToken
,
User
,
Item
from
mongoengine
import
DoesNotExist
...
...
@@ -183,3 +184,40 @@ def profile(email):
items
=
Item
.
objects
(
seller
=
user
).
all
()
return
render_template
(
'
auth/profile.html
'
,
user
=
user
,
items
=
items
)
@bp.route
(
'
/profile/<email>/token
'
,
methods
=
(
'
GET
'
,
'
POST
'
))
@login_required
def
user_access_tokens
(
email
):
"""
Show the user
'
s tokens page for the given email.
"""
user
:
User
=
get_user_by_email
(
email
)
token
=
None
if
request
.
method
==
'
POST
'
:
try
:
name
=
request
.
form
[
'
name
'
]
if
expires
:
=
request
.
form
.
get
(
'
expires
'
):
expires
=
datetime
.
fromisoformat
(
expires
)
else
:
expires
=
None
token
=
AccessToken
(
user
=
user
,
name
=
name
,
expires
=
expires
,
)
token
.
save
()
except
KeyError
as
exc
:
logger
.
debug
(
"
Missing required field: %s
"
,
exc
)
flash
(
_
(
"
Required field missing
"
))
except
Exception
as
exc
:
logger
.
exception
(
"
Error creating token: %s
"
,
exc
)
flash
(
_
(
"
Error creating token: %s
"
)
%
exc
)
else
:
flash
(
_
(
"
Created token: %s
"
)
%
token
.
name
)
return
render_template
(
'
auth/tokens.html
'
,
user
=
user
,
token
=
token
)
This diff is collapsed.
Click to expand it.
src/tjts5901/templates/auth/tokens.html
0 → 100644
+
61
−
0
View file @
2088e144
{% extends 'base.html' %}
{% block header %}
<h1>
{% block title %}Access tokens{% endblock %}
</h1>
{% endblock %}
{% block content %}
<div
class=
"container"
>
<div
class=
"row"
>
<div
class=
"col-md-4"
>
<h4>
{{_("Personal Access Tokens")}}
</h4>
<p>
{%trans%}Personal access tokens allow third-party services to authenticate with our application on your behalf.{%endtrans%}
</p>
</div>
<div
class=
"col-md-8"
>
{% if token %}
<div
class=
"alert alert-success"
role=
"alert"
>
<h4
class=
"alert-heading"
>
{{_("Your new personal access token")}}
</h4>
<p>
{%trans%}Your new personal access token is shown below. You may now use this token to make API requests.{%endtrans%}
</p>
<div
class=
"input-group mb-3"
>
<input
type=
"text"
class=
"form-control"
id=
"token"
value=
"{{ token.token }}"
readonly
>
<button
class=
"btn btn-outline-secondary"
type=
"button"
id=
"copy-token"
onclick=
"copyToken()"
>
{{_("Copy")}}
</button>
<script>
function
copyToken
()
{
var
copyText
=
document
.
getElementById
(
"
token
"
);
copyText
.
select
();
copyText
.
setSelectionRange
(
0
,
99999
);
document
.
execCommand
(
"
copy
"
);
}
</script>
</div>
<small
class=
"form-text text-muted"
>
{{ _("Make sure to copy your new token now. You won't be able to see it again!") }}
</small>
<hr>
</div>
{% endif %}
<div
class=
"card"
>
<div
class=
"card-header"
>
<div
class=
"text-center"
>
{{ _("Create access token") }}
</div>
</div>
<form
action=
"{{url_for('auth.user_access_tokens', email='me')}}"
method=
"post"
class=
"card-body"
>
<div
class=
"form-group"
>
<label
for=
"name"
>
{{ _("Name") }}
</label>
<input
type=
"text"
class=
"form-control"
name=
"name"
id=
"name"
placeholder=
"{{ _("
Enter
token
name
")
}}"
>
<div
class=
"form-text text-muted"
>
{{ _("Give your token a descriptive name so you can easily identify it in the future.") }}
</div>
</div>
<div
class=
"form-group"
>
<label
class=
"form-check-label"
for=
"expires"
>
{{ _("Expires at") }}
</label>
<input
type=
"date"
class=
"form-control"
name=
"expires"
id=
"expires"
>
<div
class=
"form-text text-muted"
>
{{ _("Leave blank to never expire.") }}
</div>
</div>
<button
type=
"submit"
class=
"btn btn-primary"
>
{{ _("Create access token") }}
</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment