Merge "update link to upper-constraints.txt"
[ccsdk/features.git] / sdnr / wt / oauth-provider / oauth-core / src / test / java / org / onap / ccsdk / features / sdnr / wt / oauthprovider / test / TestRSAAlgorithms.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.test;
23
24 import static org.junit.Assert.fail;
25 import com.auth0.jwt.JWT;
26 import com.auth0.jwt.algorithms.Algorithm;
27 import com.auth0.jwt.exceptions.JWTVerificationException;
28 import com.auth0.jwt.interfaces.JWTVerifier;
29 import java.io.IOException;
30 import java.security.Security;
31 import java.security.interfaces.RSAPrivateKey;
32 import java.security.interfaces.RSAPublicKey;
33 import java.util.Date;
34 import org.bouncycastle.jce.provider.BouncyCastleProvider;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers.RSAKeyReader;
38
39 /**
40  *
41  * @author jack
42  *
43  */
44 public class TestRSAAlgorithms {
45
46     private static final String ISSUER = "jwttest";
47     private static final String SUBJECT = "meandmymonkey";
48
49     @BeforeClass
50     public static void init() {
51         Security.addProvider(
52                 new BouncyCastleProvider()
53        );
54     }
55
56     /**
57      * private and public key were generated in ubuntu 20.04 with
58      * $ ssh-keygen -t rsa -b 4096 -m PEM -P "" -f jwtRS512.key
59      * $ openssl rsa -in jwtRS512.key -pubout -outform PEM -out jwtRS512.key.pub
60      */
61     @Test
62     public void testRSA512() {
63         RSAPrivateKey privKey = null;
64         RSAPublicKey pubKey = null;
65         try {
66             privKey = RSAKeyReader.getPrivateKey("file://src/test/resources/jwtRS512.key");
67             pubKey = RSAKeyReader.getPublicKey("file://src/test/resources/jwtRS512.key.pub");
68         } catch (IOException e) {
69             e.printStackTrace();
70             fail(e.getMessage());
71         }
72         verifyAlg(Algorithm.RSA512(pubKey, privKey));
73     }
74
75     /**
76      * private and public key were generated in ubuntu 20.04 with
77      * $ openssl genrsa 2048 -out rsa-2048bit-jwtRS256.key
78      * $ openssl rsa -in jwtRS256.key -pubout > jwtRS256.key.pub
79      */
80     @Test
81     public void testRSA256() {
82         RSAPrivateKey privKey = null;
83         RSAPublicKey pubKey = null;
84         try {
85             privKey = RSAKeyReader.getPrivateKey("file://src/test/resources/jwtRS256.key");
86             pubKey = RSAKeyReader.getPublicKey("file://src/test/resources/jwtRS256.key.pub");
87         } catch (IOException e) {
88             e.printStackTrace();
89             fail(e.getMessage());
90         }
91         verifyAlg(Algorithm.RSA512(pubKey, privKey));
92     }
93
94     private static void verifyAlg(Algorithm a) {
95         long now = new Date().getTime();
96         final String token = JWT.create().withIssuer(ISSUER).withExpiresAt(new Date(now+10000))
97                 .withIssuedAt(new Date(now))
98                 .withSubject(SUBJECT)
99                 .sign(a);
100         try {
101             JWTVerifier verifier = JWT.require(a).withIssuer(ISSUER).build();
102             verifier.verify(token);
103
104         } catch (JWTVerificationException e) {
105             fail(e.getMessage());
106         }
107     }
108 }