aboutsummaryrefslogtreecommitdiff
path: root/flakes/mediagoblin
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2023-10-04 01:35:06 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2023-10-04 02:11:48 +0200
commit1a64deeb894dc95e2645a75771732c6cc53a79ad (patch)
tree1b9df4838f894577a09b9b260151756272efeb53 /flakes/mediagoblin
parentfa25ffd4583cc362075cd5e1b4130f33306103f0 (diff)
downloadNix-1a64deeb894dc95e2645a75771732c6cc53a79ad.tar.gz
Nix-1a64deeb894dc95e2645a75771732c6cc53a79ad.tar.zst
Nix-1a64deeb894dc95e2645a75771732c6cc53a79ad.zip
Squash changes containing private information
There were a lot of changes since the previous commit, but a lot of them contained personnal information about users. All thos changes got stashed into a single commit (history is kept in a different place) and private information was moved in a separate private repository
Diffstat (limited to 'flakes/mediagoblin')
-rw-r--r--flakes/mediagoblin/bower-packages.nix8
-rw-r--r--flakes/mediagoblin/default.nix213
-rw-r--r--flakes/mediagoblin/flake.lock78
-rw-r--r--flakes/mediagoblin/flake.nix271
-rw-r--r--flakes/mediagoblin/ldap_fix.py93
-rw-r--r--flakes/mediagoblin/plugins/basicsearch/default.nix18
6 files changed, 681 insertions, 0 deletions
diff --git a/flakes/mediagoblin/bower-packages.nix b/flakes/mediagoblin/bower-packages.nix
new file mode 100644
index 0000000..03af849
--- /dev/null
+++ b/flakes/mediagoblin/bower-packages.nix
@@ -0,0 +1,8 @@
1# Generated by bower2nix v3.2.0 (https://github.com/rvl/bower2nix)
2{ fetchbower, buildEnv }:
3buildEnv { name = "bower-env"; ignoreCollisions = true; paths = [
4 (fetchbower "jquery" "2.1.4" "~2.1.3" "1ywrpk2xsr6ghkm3j9gfnl9r3jn6xarfamp99b0bcm57kq9fm2k0")
5 (fetchbower "video.js" "4.11.4" "~4.11.4" "05prdvyk0rxbkh7sdd0d9ns5l5crwvc68wzkyqmrdjw367pcv8sn")
6 (fetchbower "leaflet" "0.7.7" "~0.7.3" "0jim285bljmxxngpm3yx6bnnd10n2whwkgmmhzpcd1rdksnr5nca")
7 (fetchbower "tinymce" "4.1.10" "~4.1.7" "16jyvdb9bq8gjwhs69q8p88vdixalajrz81nsmbrzzxhkih57dyx")
8]; }
diff --git a/flakes/mediagoblin/default.nix b/flakes/mediagoblin/default.nix
new file mode 100644
index 0000000..47cc628
--- /dev/null
+++ b/flakes/mediagoblin/default.nix
@@ -0,0 +1,213 @@
1{ src, makeWrapper, stdenv, writeScript, fetchurl, buildBowerComponents, which, python36, gst_all_1, automake, autoconf, nodejs, nodePackages, lib, callPackage, fetchgit, fetchFromGitHub }:
2let
3 overridePython = let
4 packageOverrides = self: super: {
5 pybcrypt = super.buildPythonPackage rec {
6 pname = "pybcrypt";
7 version = "0.4";
8
9 src = self.fetchPypi {
10 inherit pname version;
11 sha256 = "5fa13bce551468350d66c4883694850570f3da28d6866bb638ba44fe5eabda78";
12 };
13 };
14 celery = super.celery.overridePythonAttrs(old: rec {
15 version = "3.1.26.post2";
16 src = self.fetchPypi {
17 inherit version;
18 inherit (old) pname;
19 sha256 = "5493e172ae817b81ba7d09443ada114886765a8ce02f16a56e6fac68d953a9b2";
20 };
21 patches = [];
22 doCheck = false;
23 });
24 billiard = super.billiard.overridePythonAttrs(old: rec {
25 version = "3.3.0.23";
26 src = self.fetchPypi {
27 inherit version;
28 inherit (old) pname;
29 sha256 = "02wxsc6bhqvzh8j6w758kvgqbnj14l796mvmrcms8fgfamd2lak9";
30 };
31 doCheck = false;
32 doInstallCheck = false;
33 });
34 amqp = super.amqp.overridePythonAttrs(old: rec {
35 version = "1.4.9";
36 src = self.fetchPypi {
37 inherit version;
38 inherit (old) pname;
39 sha256 = "2dea4d16d073c902c3b89d9b96620fb6729ac0f7a923bbc777cb4ad827c0c61a";
40 };
41 doCheck = false;
42 });
43 kombu = super.kombu.overridePythonAttrs(old: rec {
44 version = "3.0.37";
45 src = self.fetchPypi {
46 inherit version;
47 inherit (old) pname;
48 sha256 = "e064a00c66b4d1058cd2b0523fb8d98c82c18450244177b6c0f7913016642650";
49 };
50 propagatedBuildInputs = old.propagatedBuildInputs ++ [ self.anyjson ];
51 doCheck = false;
52 });
53 markdown = super.markdown.overridePythonAttrs(old: rec {
54 version = "3.1.1";
55 src = self.fetchPypi {
56 inherit version;
57 inherit (old) pname;
58 sha256 = "2e50876bcdd74517e7b71f3e7a76102050edec255b3983403f1a63e7c8a41e7a";
59 };
60 });
61 sqlalchemy = super.sqlalchemy.overridePythonAttrs(old: rec {
62 version = "1.1.18";
63 src = self.fetchPypi {
64 inherit version;
65 inherit (old) pname;
66 sha256 = "8b0ec71af9291191ba83a91c03d157b19ab3e7119e27da97932a4773a3f664a9";
67 };
68 doCheck = false;
69 });
70 tempita_5_3_dev = super.buildPythonPackage rec {
71 version = "47414a7-master";
72 pname = "tempita";
73 name = "${pname}-${version}";
74 src = fetchFromGitHub {
75 owner = "gjhiggins";
76 repo = "tempita";
77 rev = "47414a7c6e46a9a9afe78f0bce2ea299fa84d10d";
78 sha256 = "0f33jjjs5rvp7ar2j6ggyfykcrsrn04jaqcq71qfvycf6b7nw3rn";
79 fetchSubmodules = true;
80 };
81 buildInputs = with self; [ nose ];
82 disabled = false;
83 };
84 sqlalchemy_migrate = super.sqlalchemy_migrate.overridePythonAttrs(old: rec {
85 propagatedBuildInputs = with self; [ pbr tempita_5_3_dev decorator sqlalchemy six sqlparse ];
86 });
87 pasteScript = super.pasteScript.overridePythonAttrs(old: rec {
88 version = "2.0.2";
89 name = "PasteScript-${version}";
90 src = fetchurl {
91 url = "mirror://pypi/P/PasteScript/${name}.tar.gz";
92 sha256 = "1h3nnhn45kf4pbcv669ik4faw04j58k8vbj1hwrc532k0nc28gy0";
93 };
94 propagatedBuildInputs = with self; [ six paste PasteDeploy ];
95 });
96 werkzeug = super.werkzeug.overridePythonAttrs(old: rec {
97 version = "0.16.1";
98 src = self.fetchPypi {
99 inherit version;
100 inherit (old) pname;
101 sha256 = "b353856d37dec59d6511359f97f6a4b2468442e454bd1c98298ddce53cac1f04";
102 };
103 });
104 };
105 in
106 python36.override { inherit packageOverrides; };
107 pythonEnv = python-pkgs: with python-pkgs; [
108 waitress alembic dateutil wtforms pybcrypt
109 pytest pytest_xdist werkzeug celery
110 kombu jinja2 Babel webtest configobj markdown
111 sqlalchemy itsdangerous pytz sphinx six
112 oauthlib unidecode jsonschema PasteDeploy
113 requests PyLD exifread
114 typing pasteScript lxml
115 # For images plugin
116 pillow
117 # For video plugin
118 gst-python
119 # migrations
120 sqlalchemy_migrate
121 # authentication
122 ldap3
123 redis
124 psycopg2
125 ];
126 python = overridePython.withPackages pythonEnv;
127 gmg = writeScript "gmg" ''
128 #!${python}/bin/python
129 __requires__ = 'mediagoblin'
130 import sys
131 from pkg_resources import load_entry_point
132
133 if __name__ == '__main__':
134 sys.exit(
135 load_entry_point('mediagoblin', 'console_scripts', 'gmg')()
136 )
137 '';
138 bowerComponents = buildBowerComponents {
139 name = "mediagoblin-bower-components";
140 generated = ./bower-packages.nix;
141 inherit src;
142 };
143 pluginNames = [ "basicsearch" ];
144 allPlugins = lib.attrsets.genAttrs pluginNames
145 (name: callPackage (./plugins + "/${name}") {});
146 toPassthru = pkg: plugins: {
147 inherit allPlugins plugins;
148 withPlugins = withPlugins pkg;
149 };
150 withPlugins = pkg: toPlugins:
151 let
152 plugins = toPlugins allPlugins;
153 toBuildPlugin = n: "ln -s ${n} mediagoblin/plugins/${n.pluginName}";
154 newMediagoblin = pkg.overrideAttrs(old: {
155 postBuild = old.postBuild + "\n" + builtins.concatStringsSep "\n" (map toBuildPlugin plugins);
156 passthru = toPassthru newMediagoblin plugins;
157 });
158 in newMediagoblin;
159 package = stdenv.mkDerivation rec {
160 pname = "mediagoblin";
161 name = "${pname}-${version}";
162 version = "cd465eb-stable";
163 inherit src;
164 preConfigure = ''
165 # ./bootstrap.sh
166 aclocal -I m4 --install
167 autoreconf -fvi
168 # end
169 export HOME=$PWD
170 '';
171 configureFlags = [ "--with-python3" "--without-virtualenv" ];
172 postBuild = ''
173 cp -a ${bowerComponents}/bower_components/* extlib
174 chmod -R u+w extlib
175 make extlib
176 '';
177 installPhase = let
178 libpaths = with gst_all_1; [
179 python
180 gstreamer
181 gst-plugins-base
182 gst-libav
183 gst-plugins-good
184 gst-plugins-bad
185 gst-plugins-ugly
186 ];
187 plugin_paths = builtins.concatStringsSep ":" (map (x: "${x}/lib") libpaths);
188 typelib_paths = with gst_all_1; "${gstreamer}/lib/girepository-1.0:${gst-plugins-base}/lib/girepository-1.0";
189 in ''
190 sed -i "s/registry.has_key(current_theme_name)/current_theme_name in registry/" mediagoblin/tools/theme.py
191 sed -i -e "s@\[DEFAULT\]@[DEFAULT]\nhere = $out@" mediagoblin/config_spec.ini
192 sed -i -e "/from gi.repository import GstPbutils/s/^/gi.require_version('GstPbutils', '1.0')\n/" mediagoblin/media_types/video/transcoders.py
193 cp ${./ldap_fix.py} mediagoblin/plugins/ldap/tools.py
194 find . -name '*.pyc' -delete
195 find . -type f -exec sed -i "s|$PWD|$out|g" {} \;
196 python setup.py build
197 cp -a . $out
198 mkdir $out/bin
199 makeWrapper ${gmg} $out/bin/gmg --prefix PYTHONPATH : "$out:$PYTHONPATH" \
200 --prefix GST_PLUGIN_SYSTEM_PATH : ${plugin_paths} \
201 --prefix GI_TYPELIB_PATH : ${typelib_paths}
202 makeWrapper ${python}/bin/paster $out/bin/paster --prefix PYTHONPATH : "$out:$PYTHONPATH" \
203 --prefix GST_PLUGIN_SYSTEM_PATH : ${plugin_paths} \
204 --prefix GI_TYPELIB_PATH : ${typelib_paths}
205 makeWrapper ${python}/bin/celery $out/bin/celery --prefix PYTHONPATH : "$out:$PYTHONPATH" \
206 --prefix GST_PLUGIN_SYSTEM_PATH : ${plugin_paths} \
207 --prefix GI_TYPELIB_PATH : ${typelib_paths}
208 '';
209 buildInputs = [ makeWrapper automake autoconf which nodePackages.bower nodejs python ];
210 propagatedBuildInputs = with gst_all_1; [ python gst-libav gst-plugins-good gst-plugins-bad gst-plugins-ugly gstreamer ];
211 passthru = toPassthru package [];
212 };
213in package
diff --git a/flakes/mediagoblin/flake.lock b/flakes/mediagoblin/flake.lock
new file mode 100644
index 0000000..bba6479
--- /dev/null
+++ b/flakes/mediagoblin/flake.lock
@@ -0,0 +1,78 @@
1{
2 "nodes": {
3 "flake-utils": {
4 "locked": {
5 "lastModified": 1649676176,
6 "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=",
7 "owner": "numtide",
8 "repo": "flake-utils",
9 "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678",
10 "type": "github"
11 },
12 "original": {
13 "owner": "numtide",
14 "repo": "flake-utils",
15 "type": "github"
16 }
17 },
18 "mediagoblin": {
19 "flake": false,
20 "locked": {
21 "lastModified": 1531090939,
22 "narHash": "sha256-vSajRbuE/bu2HVsUZm25fkm/vNLXKDIK7Xn8kyKJ5Ps=",
23 "ref": "stable",
24 "rev": "cd465ebfec837a75a44c4ebd727dffe2fff6d850",
25 "revCount": 4805,
26 "submodules": true,
27 "type": "git",
28 "url": "https://git.savannah.gnu.org/git/mediagoblin.git"
29 },
30 "original": {
31 "ref": "stable",
32 "rev": "cd465ebfec837a75a44c4ebd727dffe2fff6d850",
33 "submodules": true,
34 "type": "git",
35 "url": "https://git.savannah.gnu.org/git/mediagoblin.git"
36 }
37 },
38 "myuids": {
39 "locked": {
40 "lastModified": 1,
41 "narHash": "sha256-HkW9YCLQCNBX3Em7J7MjraVEZO3I3PizkVV2QrUdULQ=",
42 "path": "../myuids",
43 "type": "path"
44 },
45 "original": {
46 "path": "../myuids",
47 "type": "path"
48 }
49 },
50 "nixpkgs": {
51 "flake": false,
52 "locked": {
53 "lastModified": 1596265691,
54 "narHash": "sha256-9ofCzFqttTsGrvTaS4RrDSTNQO9PFOz5uyn8V+2eA5M=",
55 "owner": "NixOS",
56 "repo": "nixpkgs",
57 "rev": "840c782d507d60aaa49aa9e3f6d0b0e780912742",
58 "type": "github"
59 },
60 "original": {
61 "owner": "NixOS",
62 "repo": "nixpkgs",
63 "rev": "840c782d507d60aaa49aa9e3f6d0b0e780912742",
64 "type": "github"
65 }
66 },
67 "root": {
68 "inputs": {
69 "flake-utils": "flake-utils",
70 "mediagoblin": "mediagoblin",
71 "myuids": "myuids",
72 "nixpkgs": "nixpkgs"
73 }
74 }
75 },
76 "root": "root",
77 "version": 7
78}
diff --git a/flakes/mediagoblin/flake.nix b/flakes/mediagoblin/flake.nix
new file mode 100644
index 0000000..2e821d5
--- /dev/null
+++ b/flakes/mediagoblin/flake.nix
@@ -0,0 +1,271 @@
1{
2 description = "a free software media publishing platform that anyone can run.";
3 inputs.myuids = {
4 url = "path:../myuids";
5 };
6 inputs.flake-utils.url = "github:numtide/flake-utils";
7 inputs.nixpkgs = {
8 url = "github:NixOS/nixpkgs/840c782d507d60aaa49aa9e3f6d0b0e780912742";
9 flake = false;
10 };
11 inputs.mediagoblin = {
12 url = "git+https://git.savannah.gnu.org/git/mediagoblin.git?submodules=1&ref=stable&rev=cd465ebfec837a75a44c4ebd727dffe2fff6d850";
13 flake = false;
14 };
15
16 outputs = { self, myuids, nixpkgs, mediagoblin, flake-utils }: flake-utils.lib.eachSystem ["x86_64-linux"] (system:
17 let
18 pkgs = import nixpkgs { inherit system; overlays = []; };
19 version = (builtins.fromJSON (builtins.readFile ./flake.lock)).nodes.mediagoblin.original.ref;
20 inherit (pkgs) callPackage;
21 in rec {
22 packages.mediagoblin = callPackage ./. { src = mediagoblin // { inherit version; }; };
23 defaultPackage = packages.mediagoblin;
24 legacyPackages.mediagoblin = packages.mediagoblin;
25 checks = {
26 build = defaultPackage;
27 };
28 }
29 ) // rec {
30 overlays = {
31 mediagoblin = final: prev: {
32 mediagoblin = self.defaultPackage."${final.system}";
33 };
34 };
35 overlay = overlays.mediagoblin;
36 nixosModule = { lib, pkgs, config, ... }:
37 let
38 name = "mediagoblin";
39 cfg = config.services.mediagoblin;
40
41 uid = config.ids.uids.mediagoblin;
42 gid = config.ids.gids.mediagoblin;
43
44 paste_local = pkgs.writeText "paste_local.ini" ''
45 [DEFAULT]
46 debug = false
47
48 [pipeline:main]
49 pipeline = mediagoblin
50
51 [app:mediagoblin]
52 use = egg:mediagoblin#app
53 config = ${cfg.configFile} ${cfg.package}/mediagoblin.ini
54 /mgoblin_static = ${cfg.package}/mediagoblin/static
55
56 [loggers]
57 keys = root
58
59 [handlers]
60 keys = console
61
62 [formatters]
63 keys = generic
64
65 [logger_root]
66 level = INFO
67 handlers = console
68
69 [handler_console]
70 class = StreamHandler
71 args = (sys.stderr,)
72 level = NOTSET
73 formatter = generic
74
75 [formatter_generic]
76 format = %(levelname)-7.7s [%(name)s] %(message)s
77
78 [filter:errors]
79 use = egg:mediagoblin#errors
80 debug = false
81
82 [server:main]
83 use = egg:waitress#main
84 unix_socket = ${cfg.sockets.paster}
85 unix_socket_perms = 777
86 url_scheme = https
87 '';
88 in
89 {
90 options.services.mediagoblin = {
91 enable = lib.mkEnableOption "Enable Mediagoblin’s service";
92 user = lib.mkOption {
93 type = lib.types.str;
94 default = name;
95 description = "User account under which Mediagoblin runs";
96 };
97 group = lib.mkOption {
98 type = lib.types.str;
99 default = name;
100 description = "Group under which Mediagoblin runs";
101 };
102 dataDir = lib.mkOption {
103 type = lib.types.path;
104 default = "/var/lib/${name}";
105 description = ''
106 The directory where Mediagoblin stores its data.
107 '';
108 };
109 socketsDir = lib.mkOption {
110 type = lib.types.path;
111 default = "/run/${name}";
112 description = ''
113 The directory where Mediagoblin puts runtime files and sockets.
114 '';
115 };
116 configFile = lib.mkOption {
117 type = lib.types.path;
118 description = ''
119 The configuration file path for Mediagoblin.
120 '';
121 };
122 package = lib.mkOption {
123 type = lib.types.package;
124 default = pkgs.mediagoblin;
125 example = lib.literalExample ''
126 pkgs.webapps.mediagoblin.withPlugins (p: [p.basicsearch])
127 '';
128 description = ''
129 Mediagoblin package to use.
130 '';
131 };
132 systemdStateDirectory = lib.mkOption {
133 type = lib.types.str;
134 # Use ReadWritePaths= instead if varDir is outside of /var/lib
135 default = assert lib.strings.hasPrefix "/var/lib/" cfg.dataDir;
136 lib.strings.removePrefix "/var/lib/" cfg.dataDir;
137 description = ''
138 Adjusted Mediagoblin data directory for systemd
139 '';
140 readOnly = true;
141 };
142 systemdRuntimeDirectory = lib.mkOption {
143 type = lib.types.str;
144 # Use ReadWritePaths= instead if socketsDir is outside of /run
145 default = assert lib.strings.hasPrefix "/run/" cfg.socketsDir;
146 lib.strings.removePrefix "/run/" cfg.socketsDir;
147 description = ''
148 Adjusted Mediagoblin sockets directory for systemd
149 '';
150 readOnly = true;
151 };
152 sockets = lib.mkOption {
153 type = lib.types.attrsOf lib.types.path;
154 default = {
155 paster = "${cfg.socketsDir}/mediagoblin.sock";
156 };
157 readOnly = true;
158 description = ''
159 Mediagoblin sockets
160 '';
161 };
162 pids = lib.mkOption {
163 type = lib.types.attrsOf lib.types.path;
164 default = {
165 paster = "${cfg.socketsDir}/mediagoblin.pid";
166 celery = "${cfg.socketsDir}/mediagoblin-celeryd.pid";
167 };
168 readOnly = true;
169 description = ''
170 Mediagoblin pid files
171 '';
172 };
173 };
174
175 config = lib.mkIf cfg.enable {
176 nixpkgs.overlays = [ self.overlay ];
177 users.users = lib.optionalAttrs (cfg.user == name) {
178 "${name}" = {
179 inherit uid;
180 group = cfg.group;
181 description = "Mediagoblin user";
182 home = cfg.dataDir;
183 useDefaultShell = true;
184 };
185 };
186 users.groups = lib.optionalAttrs (cfg.group == name) {
187 "${name}" = {
188 inherit gid;
189 };
190 };
191
192 systemd.slices.mediagoblin = {
193 description = "Mediagoblin slice";
194 };
195 systemd.services.mediagoblin-web = {
196 description = "Mediagoblin service";
197 wantedBy = [ "multi-user.target" ];
198 after = [ "network.target" ];
199 wants = [ "postgresql.service" "redis.service" ];
200
201 environment.SCRIPT_NAME = "/mediagoblin/";
202
203 script = ''
204 exec ./bin/paster serve \
205 ${paste_local} \
206 --pid-file=${cfg.pids.paster}
207 '';
208 preStop = ''
209 exec ./bin/paster serve \
210 --pid-file=${cfg.pids.paster} \
211 ${paste_local} stop
212 '';
213 preStart = ''
214 if [ -d ${cfg.dataDir}/plugin_static/ ]; then
215 rm ${cfg.dataDir}/plugin_static/coreplugin_basic_auth
216 ln -sf ${cfg.package}/mediagoblin/plugins/basic_auth/static ${cfg.dataDir}/plugin_static/coreplugin_basic_auth
217 fi
218 ./bin/gmg -cf ${cfg.configFile} dbupdate
219 '';
220
221 serviceConfig = {
222 Slice = "mediagoblin.slice";
223 User = cfg.user;
224 PrivateTmp = true;
225 Restart = "always";
226 TimeoutSec = 15;
227 Type = "simple";
228 WorkingDirectory = cfg.package;
229 RuntimeDirectory = cfg.systemdRuntimeDirectory;
230 StateDirectory= cfg.systemdStateDirectory;
231 PIDFile = cfg.pids.paster;
232 };
233
234 unitConfig.RequiresMountsFor = cfg.dataDir;
235 };
236
237 systemd.services.mediagoblin-celeryd = {
238 description = "Mediagoblin service";
239 wantedBy = [ "multi-user.target" ];
240 after = [ "network.target" "mediagoblin-web.service" ];
241
242 environment.MEDIAGOBLIN_CONFIG = cfg.configFile;
243 environment.CELERY_CONFIG_MODULE = "mediagoblin.init.celery.from_celery";
244
245 script = ''
246 exec ./bin/celery worker \
247 --logfile=${cfg.dataDir}/celery.log \
248 --loglevel=INFO
249 '';
250
251 serviceConfig = {
252 Slice = "mediagoblin.slice";
253 User = cfg.user;
254 PrivateTmp = true;
255 Restart = "always";
256 TimeoutSec = 60;
257 Type = "simple";
258 WorkingDirectory = cfg.package;
259 RuntimeDirectory = cfg.systemdRuntimeDirectory;
260 StateDirectory= cfg.systemdStateDirectory;
261 PIDFile = cfg.pids.celery;
262 };
263
264 unitConfig.RequiresMountsFor = cfg.dataDir;
265 };
266 };
267 };
268 };
269}
270
271
diff --git a/flakes/mediagoblin/ldap_fix.py b/flakes/mediagoblin/ldap_fix.py
new file mode 100644
index 0000000..10cc375
--- /dev/null
+++ b/flakes/mediagoblin/ldap_fix.py
@@ -0,0 +1,93 @@
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16from ldap3 import Server, Connection, SUBTREE
17from ldap3.core.exceptions import LDAPException
18import logging
19
20import six
21
22from mediagoblin.tools import pluginapi
23
24_log = logging.getLogger(__name__)
25
26
27class LDAP(object):
28 def __init__(self):
29 self.ldap_settings = pluginapi.get_config('mediagoblin.plugins.ldap')
30
31 def _connect(self, server):
32 _log.info('Connecting to {0}.'.format(server['LDAP_SERVER_URI']))
33 self.server = Server(server['LDAP_SERVER_URI'])
34
35 if 'LDAP_START_TLS' in server and server['LDAP_START_TLS'] == 'true':
36 _log.info('Initiating TLS')
37 self.server.start_tls()
38
39 def _manager_auth(self, settings, username, password):
40 conn = Connection(self.server,
41 settings['LDAP_BIND_DN'],
42 settings['LDAP_BIND_PW'],
43 auto_bind=True)
44 found = conn.search(
45 search_base=settings['LDAP_SEARCH_BASE'],
46 search_filter=settings['LDAP_SEARCH_FILTER'].format(username=username),
47 search_scope=SUBTREE,
48 attributes=[settings['EMAIL_SEARCH_FIELD']])
49 if (not found) or len(conn.entries) > 1:
50 return False, None
51
52 user = conn.entries[0]
53 user_dn = user.entry_dn
54 try:
55 email = user.entry_attributes_as_dict[settings['EMAIL_SEARCH_FIELD']][0]
56 except KeyError:
57 email = None
58
59 Connection(self.server, user_dn, password, auto_bind=True)
60
61 return username, email
62
63 def _direct_auth(self, settings, username, password):
64 user_dn = settings['LDAP_USER_DN_TEMPLATE'].format(username=username)
65 conn = Connection(self.server, user_dn, password, auto_bind=True)
66 email_found = conn.search(
67 search_base=settings['LDAP_SEARCH_BASE'],
68 search_filter='uid={0}'.format(username),
69 search_scope=SUBTREE,
70 attributes=[settings['EMAIL_SEARCH_FIELD']])
71
72 if email_found:
73 try:
74 email = conn.entries[0].entry_attributes_as_dict[settings['EMAIL_SEARCH_FIELD']][0]
75 except KeyError:
76 email = None
77
78 return username, email
79
80 def login(self, username, password):
81 for k, v in six.iteritems(self.ldap_settings):
82 try:
83 self._connect(v)
84
85 if 'LDAP_BIND_DN' in v:
86 return self._manager_auth(v, username, password)
87 else:
88 return self._direct_auth(v, username, password)
89
90 except LDAPException as e:
91 _log.info(e)
92
93 return False, None
diff --git a/flakes/mediagoblin/plugins/basicsearch/default.nix b/flakes/mediagoblin/plugins/basicsearch/default.nix
new file mode 100644
index 0000000..16be613
--- /dev/null
+++ b/flakes/mediagoblin/plugins/basicsearch/default.nix
@@ -0,0 +1,18 @@
1{ stdenv, fetchFromGitHub }:
2stdenv.mkDerivation rec {
3 name = "mediagoblin-plugin-basicsearch-${version}";
4 version = "ba0a154-master";
5 src = fetchFromGitHub {
6 owner = "ayleph";
7 repo = "mediagoblin-basicsearch";
8 rev = "ba0a1547bd24ebaf363227fe17644d38c6ce8a6b";
9 sha256 = "0d4r7xkf4gxmgaxlb264l44xbanis77g49frwfhfzsflxmdwgncy";
10 };
11 phases = "unpackPhase installPhase";
12 installPhase = ''
13 cp -R ./basicsearch $out
14 '';
15 passthru = {
16 pluginName = "basicsearch";
17 };
18}