The Basic Authentication middleware with the Iris framework, so you do not need to install it separately.

    1. Import the middleware

    2. Configure the middleware with its struct:

    1. opts := basicauth.Options{
    2. Allow: basicauth.AllowUsers(map[string]string{
    3. "username": "password",
    4. }),
    5. Realm: "Authorization Required",
    6. ErrorHandler: basicauth.DefaultErrorHandler,
    7. // [...more options]
    8. }
    1. auth := basicauth.New(opts)

    3.1 The above steps are the same as the Default function:

    3.2 Use a custom slice of Users:

    1. // The struct value MUST contain a Username and Passwords fields
    2. // or GetUsername() string and GetPassword() string methods.
    3. type User struct {
    4. Username string
    5. Password string
    6. // [...]
    7. auth := basicauth.Default([]User{...})

    3.3 Load users from a file optionally, passwords are encrypted using the golang.org/x/crypto/bcrypt package:

    1. auth := basicauth.Load("users.yml", basicauth.BCRYPT)

    Where the users.yml may look like that:

    1. - username: kataras
    2. password: $2a$10$Irg8k8HWkDlvL0YDBKLCYee6j6zzIFTplJcvZYKA.B8/clHPZn2Ey
    3. # encrypted of kataras_pass
    4. role: admin
    5. - username: makis
    6. password: $2a$10$3GXzp3J5GhHThGisbpvpZuftbmzPivDMo94XPnkTnDe7254x7sJ3O
    7. # encrypted of makis_pass
    8. role: member

    4. Register the middleware:

    1. // Register to all matched routes
    2. // OR/and register to all http error routes.
    3. app.UseError(auth)
    4. // OR register under a path prefix of a specific Party,
    5. // including all http errors of this path prefix.
    6. app.UseRouter(auth)
    7. // OR register to a specific Route before its main handler.
    8. app.Post("/protected", auth, routeHandler)

    5. Retrieve the username & password:

    1. func routeHandler(ctx iris.Context) {
    2. user := ctx.User().(*iris.SimpleUser)
    3. // user.Username

    Read more authorization and authentication examples at .