1 module vibeauth.authenticators.oauth.AuthData;
2 
3 import vibeauth.authenticators.oauth.PasswordGrantAccess;
4 import vibeauth.authenticators.oauth.RefreshTokenGrantAccess;
5 import vibeauth.authenticators.oauth.UnknownGrantAccess;
6 import vibeauth.authenticators.oauth.IGrantAccess;
7 
8 import vibe.http.server;
9 
10 import std..string;
11 
12 /// Struct used for user authentication
13 struct AuthData {
14   ///
15   string username;
16   ///
17   string password;
18   ///
19   string refreshToken;
20   /// The authorization scopes
21   string[] scopes;
22 }
23 
24 /// Get the right access generator
25 IGrantAccess getAuthData(HTTPServerRequest req) {
26   AuthData data;
27 
28   if("refresh_token" in req.form) {
29     data.refreshToken = req.form["refresh_token"];
30   }
31 
32   if("username" in req.form) {
33     data.username = req.form["username"];
34   }
35 
36   if("password" in req.form) {
37     data.password = req.form["password"];
38   }
39 
40   if("scope" in req.form) {
41     data.scopes = req.form["scope"].split(" ");
42   }
43 
44   if("grant_type" in req.form) {
45     if(req.form["grant_type"] == "password") {
46       auto grant = new PasswordGrantAccess;
47       grant.authData = data;
48 
49       return grant;
50     }
51 
52     if(req.form["grant_type"] == "refresh_token") {
53       auto grant = new RefreshTokenGrantAccess;
54       grant.authData = data;
55 
56       return grant;
57     }
58   }
59 
60   return new UnknownGrantAccess;
61 }