Skip to content

Commit bb7c077

Browse files
EFRS-1286: Added a custom change in order to set expiration date for old oauth tokens
1 parent e7407ee commit bb7c077

File tree

6 files changed

+141
-13
lines changed

6 files changed

+141
-13
lines changed

java/admin/src/main/resources/application.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ spring:
5252
database: postgresql
5353
open-in-view: true
5454
generate-ddl: false
55+
liquibase:
56+
parameters:
57+
common-client:
58+
client-id: ${app.security.oauth2.clients.COMMON.client-id}
59+
access-token-validity: ${app.security.oauth2.clients.COMMON.access-token-validity}
60+
refresh-token-validity: ${app.security.oauth2.clients.COMMON.refresh-token-validity}
61+
authorized-grant-types: ${app.security.oauth2.clients.COMMON.authorized-grant-types}
5562
mail:
5663
enable: ${ENABLE_EMAIL_SERVER:false}
5764
host: ${EMAIL_HOST:example.com}

java/admin/src/main/resources/db/changelog/db.changelog-0.2.4.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ databaseChangeLog:
33
id: expand-oauth_access_token-and-oauth_refresh_token-with-expiration-column
44
author: Volodymyr Bushko
55
changes:
6-
# delete all old tokens that do not have an expiration column
7-
- delete:
8-
tableName: oauth_access_token
9-
- delete:
10-
tableName: oauth_refresh_token
116
# add an expiration column
127
- addColumn:
138
tableName: oauth_access_token
@@ -21,6 +16,14 @@ databaseChangeLog:
2116
- column:
2217
name: expiration
2318
type: timestamp
19+
# set an oauth token expiration to avoid conflicts
20+
- customChange: {
21+
"class": "com.exadel.frs.commonservice.system.liquibase.customchange.SetOAuthTokenExpirationCustomChange",
22+
"clientId": "${common-client.client-id}",
23+
"accessTokenValidity": "${common-client.access-token-validity}",
24+
"refreshTokenValidity": "${common-client.refresh-token-validity}",
25+
"authorizedGrantTypes": "${common-client.authorized-grant-types}"
26+
}
2427
# add a non-null constraint to the expiration column
2528
- addNotNullConstraint:
2629
tableName: oauth_access_token

java/admin/src/test/resources/application.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ spring:
3333
enabled: false
3434
datasource:
3535
driver-class-name: org.postgresql.Driver
36-
url: ${POSTGRES_URL:jdbc:postgresql://compreface-postgres-db:5432/frs}
36+
url: ${POSTGRES_URL:jdbc:postgresql://localhost:5432/frs_test}
3737
username: ${POSTGRES_USER:postgres}
3838
password: ${POSTGRES_PASSWORD:postgres}
3939
jpa:
@@ -48,6 +48,13 @@ spring:
4848
database: postgresql
4949
open-in-view: true
5050
generate-ddl: false
51+
liquibase:
52+
parameters:
53+
common-client:
54+
client-id: ${app.security.oauth2.clients.COMMON.client-id}
55+
access-token-validity: ${app.security.oauth2.clients.COMMON.access-token-validity}
56+
refresh-token-validity: ${app.security.oauth2.clients.COMMON.refresh-token-validity}
57+
authorized-grant-types: ${app.security.oauth2.clients.COMMON.authorized-grant-types}
5158
mail:
5259
5360

java/api/src/test/resources/application.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spring:
77
enabled: false
88
datasource-pg:
99
driver-class-name: org.postgresql.Driver
10-
url: ${POSTGRES_URL:jdbc:postgresql://localhost:6432/frs}
10+
url: ${POSTGRES_URL:jdbc:postgresql://localhost:5432/frs_test}
1111
username: ${POSTGRES_USER:postgres}
1212
password: ${POSTGRES_PASSWORD:postgres}
1313
continue-on-error: true
@@ -62,4 +62,4 @@ logging:
6262
level:
6363
org.hibernate.SQL: DEBUG
6464
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
65-
org.hibernate.type: TRACE
65+
org.hibernate.type: TRACE

java/api/src/test/resources/db/changelog/db.changelog-0.2.4.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ databaseChangeLog:
33
id: expand-oauth_access_token-and-oauth_refresh_token-with-expiration-column
44
author: Volodymyr Bushko
55
changes:
6-
# delete all old tokens that do not have an expiration column
7-
- delete:
8-
tableName: oauth_access_token
9-
- delete:
10-
tableName: oauth_refresh_token
116
# add an expiration column
127
- addColumn:
138
tableName: oauth_access_token
@@ -21,6 +16,14 @@ databaseChangeLog:
2116
- column:
2217
name: expiration
2318
type: timestamp
19+
# set an oauth token expiration to avoid conflicts
20+
- customChange: {
21+
"class": "com.exadel.frs.commonservice.system.liquibase.customchange.SetOAuthTokenExpirationCustomChange",
22+
"clientId": "CommonClientId",
23+
"accessTokenValidity": "2400",
24+
"refreshTokenValidity": "1209600",
25+
"authorizedGrantTypes": "password,refresh_token"
26+
}
2427
# add a non-null constraint to the expiration column
2528
- addNotNullConstraint:
2629
tableName: oauth_access_token
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.exadel.frs.commonservice.system.liquibase.customchange;
2+
3+
import java.sql.Date;
4+
import java.sql.PreparedStatement;
5+
import java.sql.SQLException;
6+
import liquibase.change.custom.CustomTaskChange;
7+
import liquibase.database.Database;
8+
import liquibase.database.jvm.JdbcConnection;
9+
import liquibase.exception.CustomChangeException;
10+
import liquibase.exception.DatabaseException;
11+
import liquibase.exception.SetupException;
12+
import liquibase.exception.ValidationErrors;
13+
import liquibase.resource.ResourceAccessor;
14+
import lombok.Getter;
15+
import lombok.Setter;
16+
import lombok.extern.slf4j.Slf4j;
17+
18+
@Slf4j
19+
@Getter
20+
@Setter
21+
public class SetOAuthTokenExpirationCustomChange implements CustomTaskChange {
22+
23+
private static final String REFRESH_TOKEN = "refresh_token";
24+
25+
private static final String SET_ACCESS_TOKEN_EXPIRATION_SQL = "UPDATE oauth_access_token SET expiration = ? WHERE client_id = ?";
26+
private static final String SET_REFRESH_TOKEN_EXPIRATION_SQL = "UPDATE oauth_refresh_token SET expiration = ? WHERE token_id IN (SELECT refresh_token FROM oauth_access_token WHERE client_id = ?)";
27+
28+
private String clientId;
29+
private Integer accessTokenValidity;
30+
private Integer refreshTokenValidity;
31+
private String authorizedGrantTypes;
32+
33+
@Override
34+
public void execute(final Database database) throws CustomChangeException {
35+
try {
36+
JdbcConnection connection = (JdbcConnection) database.getConnection();
37+
setAccessTokenExpiration(connection);
38+
if (authorizedGrantTypes.contains(REFRESH_TOKEN)) {
39+
setRefreshTokenExpiration(connection);
40+
}
41+
} catch (Exception e) {
42+
log.error(e.getMessage(), e);
43+
throw new CustomChangeException(e);
44+
}
45+
}
46+
47+
private void setAccessTokenExpiration(final JdbcConnection connection)
48+
throws DatabaseException, SQLException {
49+
int updateCount = setTokenExpiration(
50+
connection,
51+
SET_ACCESS_TOKEN_EXPIRATION_SQL,
52+
accessTokenValidity
53+
);
54+
log.info(
55+
"Updated {} access tokens for client {}",
56+
updateCount,
57+
clientId
58+
);
59+
}
60+
61+
private void setRefreshTokenExpiration(final JdbcConnection connection)
62+
throws DatabaseException, SQLException {
63+
int updateCount = setTokenExpiration(
64+
connection,
65+
SET_REFRESH_TOKEN_EXPIRATION_SQL,
66+
refreshTokenValidity
67+
);
68+
log.info(
69+
"Updated {} refresh tokens for client {}",
70+
updateCount,
71+
clientId
72+
);
73+
}
74+
75+
private int setTokenExpiration(final JdbcConnection connection, final String sql, final long tokenValidity)
76+
throws DatabaseException, SQLException {
77+
try (PreparedStatement statement = connection.prepareStatement(sql)) {
78+
Date expiration = calculateExpiration(tokenValidity);
79+
statement.setDate(1, expiration);
80+
statement.setString(2, clientId);
81+
return statement.executeUpdate();
82+
}
83+
}
84+
85+
private Date calculateExpiration(final long validity) {
86+
return new Date((validity - System.currentTimeMillis()) / 1000L);
87+
}
88+
89+
@Override
90+
public String getConfirmationMessage() {
91+
return null;
92+
}
93+
94+
@Override
95+
public void setUp() throws SetupException {
96+
97+
}
98+
99+
@Override
100+
public void setFileOpener(final ResourceAccessor resourceAccessor) {
101+
102+
}
103+
104+
@Override
105+
public ValidationErrors validate(final Database database) {
106+
return null;
107+
}
108+
}

0 commit comments

Comments
 (0)