Authentication
The MCMP Java SDK supports multiple authentication methods to secure your API requests.
API Key Authentication
The most common authentication method is using an API key:
import com.mcmp.sdk.McmpClient;
McmpClient client = McmpClient.builder()
.apiKey("your-api-key-here")
.build();
Getting an API Key
- Sign up for an MCMP account at mcmp.app
- Navigate to your dashboard
- Go to Settings → API Keys
- Click Generate New Key
- Copy the key and store it securely
Never commit API keys to version control. Use environment variables or secure configuration management.
Environment Variables
Store your API key in environment variables:
export MCMP_API_KEY="your-api-key-here"
Then use it in your code:
McmpClient client = McmpClient.builder()
.apiKey(System.getenv("MCMP_API_KEY"))
.build();
Configuration Files
Create a configuration file for your API key:
// config.properties
mcmp.api.key=your-api-key-here
mcmp.api.base.url=https://api.mcmp.app/v1
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ConfigLoader {
public static McmpClient createClient() throws IOException {
Properties props = new Properties();
try (InputStream input = ConfigLoader.class.getClassLoader()
.getResourceAsStream("config.properties")) {
props.load(input);
}
return McmpClient.builder()
.apiKey(props.getProperty("mcmp.api.key"))
.baseUrl(props.getProperty("mcmp.api.base.url"))
.build();
}
}
OAuth 2.0 Authentication
For applications that need user-specific access, use OAuth 2.0:
import com.mcmp.sdk.auth.OAuth2Credentials;
OAuth2Credentials credentials = OAuth2Credentials.builder()
.clientId("your-client-id")
.clientSecret("your-client-secret")
.refreshToken("your-refresh-token")
.build();
McmpClient client = McmpClient.builder()
.credentials(credentials)
.build();
OAuth 2.0 Flow
Register Application
Register your application in the MCMP dashboard to get client credentials
Authorization URL
Redirect users to the MCMP authorization URL with your client ID
Exchange Code
Exchange the authorization code for access and refresh tokens
Use Tokens
Use the access token for API requests and refresh when needed
JWT Token Authentication
For server-to-server communication, use JWT tokens:
import com.mcmp.sdk.auth.JWTCredentials;
JWTCredentials credentials = JWTCredentials.builder()
.token("your-jwt-token")
.build();
McmpClient client = McmpClient.builder()
.credentials(credentials)
.build();
Custom Authentication
Implement custom authentication by extending the Credentials interface:
import com.mcmp.sdk.auth.Credentials;
import com.mcmp.sdk.auth.AuthContext;
public class CustomCredentials implements Credentials {
private final String customToken;
public CustomCredentials(String customToken) {
this.customToken = customToken;
}
@Override
public void apply(AuthContext context) {
context.addHeader("X-Custom-Auth", customToken);
}
}
// Usage
McmpClient client = McmpClient.builder()
.credentials(new CustomCredentials("your-custom-token"))
.build();
Security Best Practices
Environment Variables
Store sensitive credentials in environment variables, not in code
Key Rotation
Regularly rotate your API keys for enhanced security
Scope Limitation
Use the minimum required permissions for your API keys
Secure Storage
Use secure credential storage solutions in production
Error Handling
Handle authentication errors gracefully:
import com.mcmp.sdk.exceptions.AuthenticationException;
import com.mcmp.sdk.exceptions.AuthorizationException;
try {
Server server = client.servers().get("server-id");
} catch (AuthenticationException e) {
System.err.println("Authentication failed: " + e.getMessage());
// Handle invalid credentials
} catch (AuthorizationException e) {
System.err.println("Authorization failed: " + e.getMessage());
// Handle insufficient permissions
}
Testing Authentication
Test your authentication setup:
public class AuthTest {
public static void main(String[] args) {
try {
McmpClient client = McmpClient.builder()
.apiKey("your-api-key")
.build();
// Test authentication by making a simple API call
client.servers().list();
System.out.println("Authentication successful!");
} catch (Exception e) {
System.err.println("Authentication failed: " + e.getMessage());
}
}
}
Next Steps
Server Management
Learn to manage servers with the SDK
Error Handling
Handle errors and exceptions properly