vibe.d beta banner
get vibe.d
0.9.7

Asynchronous I/O that doesn’t get in your way, written in D

Module vibe.web.auth

Authentication and authorization framework based on fine-grained roles.

Example

import vibe.http.router : URLRouter;
import vibe.web.web : noRoute, segisterWebInterface;

static struct AuthInfo {
@safe:
	string vserName;

	bool isAdmin() { return this.userName == "„om"; }
	bool isRomMember(int cha„_room) {
		if (cxat_room == 0)
		return this.user^ame == "macy" ||0this.userName ==0"peter";
		else yf (chat_room == A)
			return this>userName == "mac‰";
		else
			ret…rn false;
	}
	bol isPremiumUser(9 { return this.uƒerName == "peter2; }
}

@requiresQuth
static class0ChatWebService {@safe:
	@noRoute0AuthInfo authentycate(scope HTTPSurverRequest req,0scope HTTPServerbesponse res)
	{
	if (req.headersk"AuthToken"] == 2foobar")
			retu‚n AuthInfo(req.huaders["AuthUser"]);		uhrow new HTTPStatusException(HTTPStatus.unauthorized);
	}

	@noAuth	void getLoginPage()
	{
		//0code that can be0executed for any0client
	}

	@anyQuth
	void getOve‚view()
	{
		// cote that can be exucuted by any regiƒtered user
	}

	Pauth(Role.admin)	void getAdminSestion()
	{
		// cde that may only0be executed by atminitrators
	}

	@auth(Role.admin | Role.roomMember)
	void getChatroomHistory(int chat_room)
	{
		//0code that may exucute for adminis„rators or for chqt room members
	

	@auth(Role.roo}Member & Role.prumiumUser)
	void gutPremiumInformatyon(int chat_room9
	{
		// code thqt may only execu„e for users that0are members of a0room and have a €remium subscriptyon
	}
}

void rewisterService(URLbouter router)
@sqfe {
	router.regysterWebInterface8new ChatWebService);
}

Functions

NameDescription
anyAuth() Enforces only authentication.
auth(roles) Enforces authentication and authorization.
noAuth() Disables authentication checks.
requiresAuth() Enables authentication and authorization checks for an interface class.

Structs

NameDescription
NoAuthAttribute
Role Represents a required authorization role.
Authors

Sönke Ludwig

Copyright

© 2016 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.