Skip to content

Commit

Permalink
[api/jwt] add expiration date for token
Browse files Browse the repository at this point in the history
  • Loading branch information
adfaure committed Nov 14, 2023
1 parent c917234 commit 01e1da4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions etc/oar.conf
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ OARSTAT_DEFAULT_OUTPUT_FORMAT=2
# openssl rand -hex 32
API_SECRET_KEY="3f22a0a65212bfb6cdf0dc4b39be189b3c89c6c2c8ed0d1655e0df837145208b"
API_SECRET_ALGORITHM="HS256"
API_ACCESS_TOKEN_EXPIRE_MINUTES = 524160 # One year


# Disable this if you are not ok with a simple pidentd "authentication"
# It is safe enough if you fully trust the client hosts (with an apropriate
Expand Down
5 changes: 4 additions & 1 deletion oar/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def __call__(self, scope, receive, send):


def create_app(
config: Optional[Configuration] = None, engine=None, root_path: Optional[str] = None, logger=None
config: Optional[Configuration] = None,
engine=None,
root_path: Optional[str] = None,
logger=None,
):
"""Return the OAR API application instance."""
app = FastAPI(root_path=root_path)
Expand Down
3 changes: 1 addition & 2 deletions oar/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@


def get_user(
credentials: Annotated[str, Depends(oauth2_scheme)],
config=Depends(get_config)
credentials: Annotated[str, Depends(oauth2_scheme)], config=Depends(get_config)
) -> Optional[str]:

username = None
Expand Down
5 changes: 2 additions & 3 deletions oar/api/routers/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from passlib.apache import HtpasswdFile

from oar import VERSION

# from oar.lib.globals import get_logger
from oar.lib.configuration import Configuration

Expand Down Expand Up @@ -99,7 +100,5 @@ def authentication(

@router.get("/me")
async def read_users_me(current_user: Annotated[str, Depends(get_user)]):
data = {
"user": current_user
}
data = {"user": current_user}
return data
19 changes: 12 additions & 7 deletions oar/lib/access_token.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from datetime import datetime
from datetime import datetime, timedelta

from jose import jwt
from oar.lib.configuration import Configuration

# SECRET_KEY = "3f22a0a65212bfb6cdf0dc4b39be189b3c89c6c2c8ed0d1655e0df837145208b"
ALGORITHM = "HS256"
from oar.lib.configuration import Configuration


def create_access_token(data: dict, config: Configuration) -> str:
to_encode = data.copy()
to_encode.update({"date": f"{datetime.utcnow()}"})

now = datetime.utcnow()
exp_minutes = int(config.get("API_ACCESS_TOKEN_EXPIRE_MINUTES"))
expires_delta = timedelta(minutes=exp_minutes)

expire = now + expires_delta
to_encode.update({"exp": expire, "date": f"{now}"})

# to get a string like this run:
# openssl rand -hex 32
SECRET_KEY = config.get("API_SECRET_KEY", None)
secret_key = config.get("API_SECRET_KEY", None)
algorithm = config.get("API_SECRET_ALGORITHM", None)

encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
encoded_jwt = jwt.encode(to_encode, secret_key, algorithm=algorithm)
return encoded_jwt

0 comments on commit 01e1da4

Please sign in to comment.