View Javadoc
1   package com.guinetik.rr.auth;
2   
3   import java.util.Map;
4   import java.util.function.BooleanSupplier;
5   
6   /**
7    * Factory for creating {@link AuthStrategy} instances.
8    *
9    * <p>This factory provides static methods to create various authentication strategies
10   * without directly instantiating the strategy classes. It's the recommended way to
11   * create authentication strategies for use with {@link com.guinetik.rr.RocketRestConfig}.
12   *
13   * <h2>No Authentication</h2>
14   * <pre class="language-java"><code>
15   * AuthStrategy noAuth = AuthStrategyFactory.createNoAuth();
16   * </code></pre>
17   *
18   * <h2>Basic Authentication</h2>
19   * <pre class="language-java"><code>
20   * AuthStrategy basic = AuthStrategyFactory.createBasicAuth("username", "password");
21   * </code></pre>
22   *
23   * <h2>Bearer Token</h2>
24   * <pre class="language-java"><code>
25   * // Simple bearer token
26   * AuthStrategy bearer = AuthStrategyFactory.createBearerToken("my-api-token");
27   *
28   * // Bearer token with custom refresh logic
29   * AuthStrategy refreshable = AuthStrategyFactory.createBearerToken("initial-token", () -&gt; {
30   *     // Custom refresh logic
31   *     String newToken = fetchNewTokenFromServer();
32   *     return newToken != null;
33   * });
34   * </code></pre>
35   *
36   * <h2>OAuth 2.0 Client Credentials</h2>
37   * <pre class="language-java"><code>
38   * AuthStrategy oauth = AuthStrategyFactory.createOAuth2ClientCredentials(
39   *     "client-id",
40   *     "client-secret",
41   *     "https://auth.example.com/oauth/token"
42   * );
43   *
44   * // With additional parameters (e.g., scope)
45   * Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
46   * params.put("scope", "read write");
47   * AuthStrategy oauthWithScope = AuthStrategyFactory.createOAuth2ClientCredentials(
48   *     "client-id", "client-secret", "https://auth.example.com/oauth/token", params
49   * );
50   * </code></pre>
51   *
52   * <h2>OAuth 2.0 Password Grant</h2>
53   * <pre class="language-java"><code>
54   * AuthStrategy password = AuthStrategyFactory.createOAuth2Password(
55   *     "user@example.com",
56   *     "userPassword",
57   *     "https://auth.example.com/oauth/token"
58   * );
59   * </code></pre>
60   *
61   * @author guinetik &lt;guinetik@gmail.com&gt;
62   * @see AuthStrategy
63   * @see com.guinetik.rr.RocketRestConfig
64   * @since 1.0.0
65   */
66  public class AuthStrategyFactory {
67      
68      /**
69       * Creates a strategy that does not perform authentication.
70       * 
71       * @return a new no-auth strategy
72       */
73      public static AuthStrategy createNoAuth() {
74          return new NoAuthStrategy();
75      }
76      
77      /**
78       * Creates a strategy that uses HTTP Basic authentication.
79       * 
80       * @param username the username
81       * @param password the password
82       * @return a new basic auth strategy
83       */
84      public static AuthStrategy createBasicAuth(String username, String password) {
85          return new BasicAuthStrategy(username, password);
86      }
87      
88      /**
89       * Creates a strategy that uses Bearer token authentication.
90       * 
91       * @param token the bearer token
92       * @return a new bearer token strategy
93       */
94      public static AuthStrategy createBearerToken(String token) {
95          return new BearerTokenStrategy(token);
96      }
97      
98      /**
99       * Creates a strategy that uses Bearer token authentication with custom refresh logic.
100      * 
101      * @param token             the bearer token
102      * @param refreshTokenLogic a {@link BooleanSupplier} that dictates the behavior of token refresh.
103      *                          It should return {@code true} if the token was successfully refreshed, {@code false} otherwise.
104      * @return a new bearer token strategy with custom refresh logic
105      */
106     public static AuthStrategy createBearerToken(String token, BooleanSupplier refreshTokenLogic) {
107         return new BearerTokenStrategy(token, refreshTokenLogic);
108     }
109     
110     /**
111      * Creates a strategy that uses OAuth 2.0 client credentials flow.
112      * 
113      * @param clientId the OAuth 2.0 client ID
114      * @param clientSecret the OAuth 2.0 client secret
115      * @return a new OAuth 2.0 client credentials strategy
116      */
117     public static AuthStrategy createOAuth2ClientCredentials(String clientId, String clientSecret, String tokenUrl) {
118         return new OAuth2ClientCredentialsStrategy(clientId, clientSecret, tokenUrl);
119     }
120     
121     /**
122      * Creates a strategy that uses OAuth 2.0 client credentials flow with additional parameters.
123      * 
124      * @param clientId the OAuth 2.0 client ID
125      * @param clientSecret the OAuth 2.0 client secret
126      * @param tokenUrl the token endpoint URL
127      * @param additionalParams additional parameters to include in the token request
128      * @return a new OAuth 2.0 client credentials strategy
129      */
130     public static AuthStrategy createOAuth2ClientCredentials(String clientId, String clientSecret, String tokenUrl,
131                                                            Map<String, String> additionalParams) {
132         return new OAuth2ClientCredentialsStrategy(clientId, clientSecret, tokenUrl, additionalParams);
133     }
134     
135     /**
136      * Creates a strategy that uses OAuth 2.0 password flow.
137      * 
138      * @param username the user's username
139      * @param password the user's password
140      * @return a new OAuth 2.0 password strategy
141      */
142     public static AuthStrategy createOAuth2Password(String username, String password, String tokenUrl) {
143         return new OAuth2PasswordStrategy(username, password, null, null, tokenUrl);
144     }
145     
146     /**
147      * Creates a strategy that uses OAuth 2.0 password flow with client credentials.
148      * 
149      * @param username the user's username
150      * @param password the user's password
151      * @param clientId the OAuth 2.0 client ID
152      * @param clientSecret the OAuth 2.0 client secret
153      * @return a new OAuth 2.0 password strategy
154      */
155     public static AuthStrategy createOAuth2Password(String username, String password, 
156                                                   String clientId, String clientSecret, String tokenUrl) {
157         return new OAuth2PasswordStrategy(username, password, clientId, clientSecret, tokenUrl);
158     }
159     
160     /**
161      * Creates a strategy that uses OAuth 2.0 password flow with client credentials and additional parameters.
162      * 
163      * @param username the user's username
164      * @param password the user's password
165      * @param clientId the OAuth 2.0 client ID
166      * @param clientSecret the OAuth 2.0 client secret
167      * @param tokenUrl the token endpoint URL
168      * @param additionalParams additional parameters to include in the token request
169      * @return a new OAuth 2.0 password strategy
170      */
171     public static AuthStrategy createOAuth2Password(String username, String password, 
172                                                   String clientId, String clientSecret,
173                                                   String tokenUrl,
174                                                   Map<String, String> additionalParams) {
175         return new OAuth2PasswordStrategy(username, password, clientId, clientSecret, tokenUrl, additionalParams);
176     }
177 
178     /**
179      * Creates a strategy that uses OAuth 2.0 assertion flow.
180      * This can be used with various identity providers like SAP, Azure AD, Okta, etc.
181      * 
182      * @param clientId       the OAuth client ID
183      * @param userId         the user ID
184      * @param privateKey     the private key for assertion
185      * @param companyId      the company ID (optional, can be null)
186      * @param grantType      the OAuth grant type
187      * @param assertionUrl   the assertion endpoint URL
188      * @param tokenUrl       the token endpoint URL
189      * @return a new OAuth 2.0 assertion strategy
190      */
191     public static AuthStrategy createOAuth2Assertion(String clientId, String userId, String privateKey,
192                                            String companyId, String grantType, String assertionUrl, String tokenUrl) {
193         return new OAuth2AssertionStrategy(clientId, userId, privateKey, companyId, grantType, assertionUrl, tokenUrl);
194     }
195 
196     /**
197      * Creates a strategy that uses OAuth 2.0 assertion flow with additional parameters.
198      * This can be used with various identity providers like SAP, Azure AD, Okta, etc.
199      * 
200      * @param clientId                 the OAuth client ID
201      * @param userId                   the user ID
202      * @param privateKey               the private key for assertion
203      * @param companyId                the company ID (optional, can be null)
204      * @param grantType                the OAuth grant type
205      * @param assertionUrl             the assertion endpoint URL
206      * @param tokenUrl                 the token endpoint URL
207      * @param additionalAssertionParams additional parameters for assertion request
208      * @param additionalTokenParams    additional parameters for token request
209      * @return a new OAuth 2.0 assertion strategy
210      */
211     public static AuthStrategy createOAuth2Assertion(String clientId, String userId, String privateKey,
212                                            String companyId, String grantType, String assertionUrl, String tokenUrl,
213                                            Map<String, String> additionalAssertionParams,
214                                            Map<String, String> additionalTokenParams) {
215         return new OAuth2AssertionStrategy(clientId, userId, privateKey, companyId, grantType, assertionUrl, tokenUrl,
216                                  additionalAssertionParams, additionalTokenParams);
217     }
218 }