-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API to generate and store tokens
- Loading branch information
Showing
11 changed files
with
656 additions
and
6 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
47 changes: 47 additions & 0 deletions
47
java/code/src/com/redhat/rhn/frontend/xmlrpc/InvalidTokenException.java
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,47 @@ | ||
/* | ||
* Copyright (c) 2024 SUSE LLC | ||
* | ||
* This software is licensed to you under the GNU General Public License, | ||
* version 2 (GPLv2). There is NO WARRANTY for this software, express or | ||
* implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 | ||
* along with this software; if not, see | ||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
*/ | ||
package com.redhat.rhn.frontend.xmlrpc; | ||
|
||
import com.redhat.rhn.FaultException; | ||
|
||
/** | ||
* Token creation failed. | ||
* | ||
*/ | ||
public class InvalidTokenException extends FaultException { | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public InvalidTokenException() { | ||
super(11000 , "invalidToken" , "Invalid token"); | ||
} | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param message exception message | ||
*/ | ||
public InvalidTokenException(String message) { | ||
super(11000 , "invalidToken" , message); | ||
} | ||
|
||
/** | ||
* Constructor | ||
* @param cause the cause (which is saved for later retrieval | ||
* by the Throwable.getCause() method). (A null value is | ||
* permitted, and indicates that the cause is nonexistent or | ||
* unknown.) | ||
*/ | ||
public InvalidTokenException(Throwable cause) { | ||
super(11000 , "invalidToken" , "Invalid token", cause); | ||
} | ||
} |
8 changes: 2 additions & 6 deletions
8
...activationkey/TokenCreationException.java → ...ontend/xmlrpc/TokenCreationException.java
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,86 @@ | ||
/* | ||
* Copyright (c) 2024 SUSE LLC | ||
* | ||
* This software is licensed to you under the GNU General Public License, | ||
* version 2 (GPLv2). There is NO WARRANTY for this software, express or | ||
* implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 | ||
* along with this software; if not, see | ||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
*/ | ||
|
||
package com.suse.manager.model.hub; | ||
|
||
import com.redhat.rhn.common.conf.ConfigDefaults; | ||
|
||
import com.suse.manager.webui.utils.token.IssTokenBuilder; | ||
import com.suse.manager.webui.utils.token.Token; | ||
import com.suse.manager.webui.utils.token.TokenBuildingException; | ||
import com.suse.manager.webui.utils.token.TokenException; | ||
import com.suse.manager.webui.utils.token.TokenParser; | ||
import com.suse.manager.webui.utils.token.TokenParsingException; | ||
|
||
/** | ||
* Business logic to manage ISSv3 Sync | ||
*/ | ||
public class HubManager { | ||
|
||
private final HubFactory hubFactory; | ||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public HubManager() { | ||
this(new HubFactory()); | ||
} | ||
|
||
/** | ||
* Builds an instance with the given dependencies | ||
* @param hubFactoryIn the hub factory | ||
*/ | ||
public HubManager(HubFactory hubFactoryIn) { | ||
this.hubFactory = hubFactoryIn; | ||
} | ||
|
||
/** | ||
* Create a new access token for the given FQDN and store it in the database | ||
* @param fqdn the FQDN of the peripheral/hub | ||
* @return the serialized form of the token | ||
* @throws TokenBuildingException when an error occurs during generation | ||
* @throws TokenParsingException when the generated token cannot be parsed | ||
*/ | ||
public String issueAccessToken(String fqdn) throws TokenException { | ||
Token token = new IssTokenBuilder(fqdn) | ||
.usingServerSecret() | ||
.build(); | ||
|
||
hubFactory.saveToken(fqdn, token.getSerializedForm(), TokenType.ISSUED, token.getExpirationTime()); | ||
return token.getSerializedForm(); | ||
} | ||
|
||
/** | ||
* Stores in the database the access token of the given FQDN | ||
* @param fqdn the FQDN of the peripheral/hub that generated this token | ||
* @param token the token | ||
* @throws TokenParsingException when it's not possible to process the token | ||
*/ | ||
public void storeAccessToken(String fqdn, String token) throws TokenParsingException { | ||
// We do not need to verify the signature as this token is for accessing another system. | ||
// That system will take care of ensuring its authenticity | ||
Token parsedToken = new TokenParser() | ||
.skippingSignatureVerification() | ||
.verifyingExpiration() | ||
.verifyingNotBefore() | ||
.parse(token); | ||
|
||
// Verify if this token is for this system | ||
String targetFqdn = parsedToken.getClaim("fqdn", String.class); | ||
String hostname = ConfigDefaults.get().getHostname(); | ||
|
||
if (targetFqdn == null || !targetFqdn.equals(hostname)) { | ||
throw new TokenParsingException("FQDN do not match. Expected %s got %s".formatted(hostname, targetFqdn)); | ||
} | ||
|
||
hubFactory.saveToken(fqdn, token, TokenType.CONSUMED, parsedToken.getExpirationTime()); | ||
} | ||
} |
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.