Skip to content

Instantly share code, notes, and snippets.

@ranfysvalle02
Created February 2, 2024 05:06
Show Gist options
  • Save ranfysvalle02/a51342278d3b10671c25d6c12bbf6909 to your computer and use it in GitHub Desktop.
Save ranfysvalle02/a51342278d3b10671c25d6c12bbf6909 to your computer and use it in GitHub Desktop.
MongoDB Atlas App Services Auth + OSO Cloud
exports = async function(arg){
const axios = require('axios').default;
const OSO_authToken = String(context.values.get("Value-OSO_API")); // Replace with your actual authorization token
const OSO_userId = context.user.data.email;
let checkOSO = async function(){
const apiUrl = 'https://cloud.osohq.com/api/list';
const requestData = {
actor_type: "User",
actor_id: OSO_userId,
action: "view",
resource_type: "Account",
context_facts: [],
};
try {
const response = await axios.post(apiUrl, requestData, {
headers: {
Authorization: `Bearer ${OSO_authToken}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
console.log('Response:', response.data);
return (response.data.results);
} catch (error) {
console.error('Error:', error);
return error;
}
}
let myAccounts = await checkOSO();
var serviceName = "mongodb-atlas";
var dbName = "eventlab-dev";
var collName = "events";
var collection = context.services.get(serviceName).db(dbName).collection(collName);
return await collection.aggregate([
{"$match":{
"account_id":{"$in":myAccounts}
}},
{
"$group": {
"_id": "$account_name",
"count": { "$sum": 1 } // Count the number of documents in each group
}
}
])
};
@ranfysvalle02
Copy link
Author

OSO Config:

actor User {}


resource Account {

  roles = ["viewer", "owner"];

  permissions = ["view", "edit"];


  "view" if "viewer";

  "edit" if "owner";

  "viewer" if "owner";

}


test "Account roles and permissions" {

  setup {

    has_role(User{"alice"}, "viewer", Account{"example"});

    has_role(User{"bob"}, "owner", Account{"example"});

  }

  assert     allow(User{"alice"}, "view", Account{"example"});

  assert     allow(User{"bob"}, "view", Account{"example"});

  assert_not allow(User{"alice"}, "edit", Account{"example"});

  assert     allow(User{"bob"}, "edit", Account{"example"});

}

@ranfysvalle02
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment