Browse Source

add crypto library, begin writing createUser function

master
James Fenn 6 months ago
parent
commit
d7ddd3090b
6 changed files with 60 additions and 5 deletions
  1. +1
    -0
      dub.json
  2. +2
    -0
      dub.selections.json
  3. +1
    -1
      source/daemon.d
  4. +2
    -2
      source/model/instance.d
  5. +12
    -2
      source/model/user.d
  6. +42
    -0
      source/service/users.d

+ 1
- 0
dub.json View File

@@ -4,6 +4,7 @@
"Sean Bailey"
],
"dependencies": {
"crypto": "~>0.2.13",
"ddbus": "~>2.3.0",
"jsonizer": "~master"
},


+ 2
- 0
dub.selections.json View File

@@ -2,8 +2,10 @@
"fileVersion": 1,
"versions": {
"asdf": "0.5.7",
"crypto": "0.2.13",
"ddbus": "2.3.0",
"dunit": "1.0.16",
"intel-intrinsics": "1.2.3",
"jsonizer": "~master"
}
}

+ 1
- 1
source/daemon.d View File

@@ -94,7 +94,7 @@ string daemonCall(string name, string data) {
// call dbus method
User user;
user.id = to!string(getlogin());
user.name = to!string(getlogin());
string resultJson = daemon_proxy.call!string(name, user.toJSONString(), data);

// parse JSON status/result


+ 2
- 2
source/model/instance.d View File

@@ -11,8 +11,8 @@ struct Instance {

@jsonize {
string name; // project git name
string owner; // project owner name
int[] ports; // reserved port numbers (or null)
string owner; // project owner name
int[] ports; // reserved port numbers (or null)
}

bool opEquals(Instance other) {


+ 12
- 2
source/model/user.d View File

@@ -7,10 +7,20 @@ struct User {
mixin JsonizeMe;

@jsonize {
string id; // user id
string name; // username
string[] instances; // owned projects
bool admin;
}

string id() {
return name;
}

bool opEquals(User other) {
return this.id != null && this.id == other.id;
return this.name != null && this.name == other.name;
}

string getPath() {
return "/home/" ~ name;
}
}

+ 42
- 0
source/service/users.d View File

@@ -0,0 +1,42 @@
module service.users;

import config.global : GlobalConfig;
import model.user : User;
import jsonizer.tojson;

import std.file : isDir, mkdir, write, FileException;
import core.stdc.errno : EEXIST;

import crypto.rsa;

/**
* Args: (string) username
* Returns: (User) created user
*/
string createUser(GlobalConfig conf, User caller, string username) {
if (!caller.admin)
throw new Exception("Only administrators can create a new user!");
User user;
user.name = username;

if (!isDir(user.getPath()))
throw new Exception("The user directory doesn't exist! " ~ user.getPath());
string horrificDir = user.getPath() ~ "/.horrific";
try {
mkdir(horrificDir);
} catch (FileException e) {
// only catch exception if EEXIST (dir already exists)
if (e.errno != EEXIST)
throw e;
}

RSAKeyPair keyPair = RSA.generateKeyPair(1024);
write(horrificDir ~ "/id_rsa", keyPair.privateKey);
write(horrificDir ~ "/id_rsa_pub", keyPair.publicKey);

conf.users.insert(user.id(), user);
conf.users.save();
return user.toJSONString();
}

Loading…
Cancel
Save