ogi-sdk
v1.0.5
Published
A comprehensive SDK for aggregating and accessing OpenGateway APIs, complete with versatile authentication methods.
Downloads
39
Maintainers
Readme
Getting Started Guide for SDK
Introduction
This SDK is designed for interactions with a specific API, offering functionalities like authentication, device location verification, and network information retrieval.
Prerequisites
- Node.js environment.
- Client credentials (client ID and client secret) for authentication.
Installation
- If the SDK is available as an npm package, install it using
npm install ogi-sdk
Initialization
import { OGISDK } from 'ogi-sdk';
// Initialize with your client credentials
const config = {
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
redirectUri: 'your-redirect-uri'
};
const sdk = new OGISDK(config);
Authentication
- Two-legged OAuth for server-to-server communication:
const session = await sdk.auth.get2LeggedToken(['your-scope']);
- Three-legged OAuth for user authentication:
const authUrl = await sdk.auth.get3LeggedAuthUrl({
scopes: ['your-scope'],
purpose: 'your-purpose'
});
// Redirect user to authUrl for authentication
- Exchanging authorization code for a token:
const session = await sdk.auth.getTokenfromCode('authorization-code');
Request Client
- Making requests using
RequestClient
:
const response = await sdk.requestClient.makeRequest('/your-api-endpoint', REQ_METHOD.GET, null, { session });
Device Location Verification
const location = {
lat: 52.5200,
lng: 13.4050,
accuracy: 100,
radius: 200,
deviceId: 'your-device-id',
deviceIdType: 'phoneNumber' // or 'ipv4Address', 'ipv6Address', 'networkAccessIdentifier'
};
const isLocationVerified = await sdk.verifyLocation(location);
SIM Swap
- Retrieve the date of the last SIM swap for a given phone number:
const lastSimSwapDate = await sdk.getLastSimSwapDate('your-phone-number');
- Check if a SIM swap has been performed during a specified past period:
const hasSimSwapped = await sdk.checkIfSimSwapped({
phoneNumber: 'your-phone-number',
maxAge: 24 // Hours to check in the past
});
Example Usage - CIBA
Here's an example showing how to use the SDK for device location verification and authentication:
async function main() {
const sdk = new OGISDK({
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
redirectUri: 'your-redirect-uri'
});
// Authenticate and get a session token
const authRes = await sdk.auth.cibaAuthLoginHint({
purpose: ['purpose'],
loginHint: 'hint'
});
const session = await sdk.auth.pollCibaToken(authRes.authReqId, 2);
// Verify device location
const location = {
lat: 52.5200, // Latitude
lng: 13.4050, // Longitude
deviceId: '33699901031', // Device ID
deviceIdType: 'phoneNumber' // Device ID type
};
const isLocationVerified = await sdk.verifyLocation(location);
console.log('Is location verified:', isLocationVerified);
// Check SIM Swap
const lastSimSwapDate = await sdk.getLastSimSwapDate('123456789');
console.log('Last SIM swap date:', lastSimSwapDate);
const hasSimSwapped = await sdk.checkIfSimSwapped({
phoneNumber: '123456789',
maxAge: 24
});
console.log('Has SIM swapped in the last 24 hours:', hasSimSwapped);
}
main().catch(console.error);
Example Usage with get3LeggedAuthUrl (SERVER)
Here's an example showing how to use the SDK in case you are using the 3legged auth flow in development phase and need an existing public server for the redirect URL
const app = express();
const port = 3000;
// Initialize your SDK
const sdk = new OGISDK({
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
});
// Redirect to Auth route
app.get('/redirectToAuth', async (req: Request, res: Response) => {
const authURL = await sdk.auth.get3LeggedAuthUrl({
scopes: ['openid'],
purpose: ['dpv:FraudPreventionAndDetection:location-verification'],
});
res.redirect(authURL);
});
// Callback route
app.get('/callback', async (req: Request, res: Response) => {
const { code } = req.query; // Assuming your auth service redirects back with a "code" parameter
if (!code) {
return res.status(400).send('Authorization code is missing');
}
const session = await sdk.auth.getTokenfromCode(code);
// Store the token in the session
// call here to any other SDK API
res.send('You are logged in');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});