This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type User struct { | |
| Id string `json:"id"` | |
| Email string `json:"email,omitempty"` | |
| CreatedAt time.Time `json:"created_at"` | |
| UpdatedAt time.Time `json:"updated_at"` | |
| Name string `json:"name,omitempty"` | |
| Admin bool `json:"admin"` | |
| Active bool `json:"-"` | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface UserDAO { | |
| @SqlQuery("SELECT u.id, u.email, u.password_hash, u.apikey, u.created_at, u.updated_at, u.name, u.id, u.admin, u.active from users u where u.apikey = :apiKey") | |
| @RegisterMapper(UserMapper.class) | |
| User getUserByApiKey(@Bind("apiKey") String apiKey); | |
| static class UserMapper implements ResultSetMapper<User> { | |
| @Override | |
| public User map(int i, ResultSet rs, StatementContext statementContext) throws SQLException { | |
| return new User( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Credentials string | |
| func Authenticate(w http.ResponseWriter, r *http.Request) { | |
| credentials, err := newCredentials(r) | |
| if err != nil { | |
| http.Error(w, err.Error(), BadRequest) | |
| return | |
| } | |
| user, err := GetUserByApiKey(db, credentials) | |
| if err != nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| http.HandleFunc("/authenticate", Authenticate) | |
| http.ListenAndServe(":8080", nil) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Path("authenticate") | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public class AuthResource { | |
| private final UserDAO userDAO; | |
| public AuthResource(UserDAO userDAO) { | |
| this.userDAO = userDAO; | |
| } | |
| @GET |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Config struct { | |
| Db struct { | |
| Name string | |
| User string | |
| Password string | |
| } | |
| } | |
| func ReadConfig() Config { | |
| if len(os.Args) != 2 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "Db": { | |
| "Name": "authdb", | |
| "User": "postgres", | |
| "Password": "postgres" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| database: | |
| driverClass: org.postgresql.Driver | |
| user: postgres | |
| password: postgres | |
| url: jdbc:postgresql://localhost/authdb | |
| properties: | |
| charSet: UTF-8 | |
| http: | |
| requestLog: | |
| console: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AuthConfiguration extends Configuration { | |
| @Valid | |
| @NotNull | |
| @JsonProperty | |
| private DatabaseConfiguration database = new DatabaseConfiguration(); | |
| public DatabaseConfiguration getDatabaseConfiguration() { | |
| return database; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Path("authenticate") | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public class AuthResource { | |
| private final UserDAO userDAO; | |
| public AuthResource(UserDAO userDAO) { | |
| this.userDAO = userDAO; | |
| } | |
| @GET |