cz.alej.michalik.totp.server.Users.java Source code

Java tutorial

Introduction

Here is the source code for cz.alej.michalik.totp.server.Users.java

Source

/*
   Copyright 2017 Petr Michalk
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cz.alej.michalik.totp.server;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Properties;

import org.apache.commons.codec.binary.Base32;
import org.json.simple.JSONObject;
import org.restlet.data.Status;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

/**
 * Tda pro sprvu databze uivatel
 * 
 * @author Petr Michalk
 * @see org.restlet.resource.ServerResource
 *
 */
public class Users extends ServerResource {

    Properties p = Data.load();
    JSONObject msg = new JSONObject();

    // HTTP kdy
    Status error = new Status(404);
    Status conflict = new Status(409);
    Status success = new Status(200);
    Status created = new Status(201);

    /**
     * Nastav zprvu
     */
    public void doInit() {
        // Pokud metoda nezmn stav, stala se chyba
        msg = new JSONObject();
        msg.put("status", "error");
    }

    /**
     * Vytvo nov zznam a vrt index zznamu a kl?
     * 
     * @return JSON
     */
    @Post
    public String create(String user) {
        this.setStatus(error);
        if (user == null) {
            msg.put("message", "No username");
        } else if (user.matches("^[a-zA-Z0-9_]+$") == false) {
            // Uivatelsk jmno sm obsahovat pouze psmena, ?sla nebo
            // podtrtka
            msg.put("message", "Data should be '[a-zA-Z0-9_]+'");
        } else {
            // Vygeneruju pseudonhodn heslo
            String secret = generateSecret();
            // Pokud existuje uivatelsk jmno
            if (!Data.add(user, secret, false)) {
                this.setStatus(conflict);
                msg.put("message", "Username already exists");
            } else {
                this.setStatus(created);
                this.setLocationRef("./users/" + user);
                msg.put("status", "ok");
                msg.put("username", user);
                msg.put("secret", secret);
            }
        }
        return msg.toJSONString();
    }

    /**
     * Vrt po?et uivatel
     * 
     * @return JSON
     */
    @Get
    public String getUsers() {
        // Zprva pro klienta je JSON
        this.setStatus(success);
        p = Data.load();
        msg.put("status", "ok");
        msg.put("users", String.valueOf(p.size()));
        return msg.toJSONString();
    }

    /**
     * Vymae vechny zznamy
     */
    @Delete
    public StringRepresentation delete() {
        Data.deleteAll();
        msg.put("status", "ok");
        return new StringRepresentation(msg.toJSONString());
    }

    /**
     * Vygenerovat kl? pro generovn TOTP hesla
     * 
     * @return base32 etzec
     */
    public static String generateSecret() {
        // Chci kl? o velikosti 20 byt
        int maxBits = 20 * 8 - 1;
        SecureRandom rand = new SecureRandom();
        byte[] val = new BigInteger(maxBits, rand).toByteArray();
        return new Base32().encodeToString(val);
    }

}