> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/authentication.md).

# Authentication

{% embed url="<https://youtu.be/gYwO3Jbi4O4>" %}
Authentication
{% endembed %}

The target is a wallet contract `Wallet.sol`:

```solidity
pragma solidity ^0.8.18;

contract Wallet {
    address payable public owner;

    constructor() payable {
        owner = payable(msg.sender);
    }

    receive() external payable {}

    function withdraw(uint256 _amount) external {
        require(msg.sender == owner, "caller is not owner");
        payable(msg.sender).transfer(_amount);
    }

    function setOwner(address _owner) external {
        require(msg.sender == owner, "caller is not owner");
        owner = payable(_owner);
    }
}
```

Create a test file `Auth.t.sol`:

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import "forge-std/Test.sol";
import {Wallet} from "../src/Wallet.sol";

contract AuthTest is Test {
    Wallet public wallet;

    function setUp() public {
        wallet = new Wallet();
    }

    function testSetOwner() public {
        wallet.setOwner(address(1));
        assertEq(wallet.owner(), address(1));
    }
}
```

This `AuthTest` contract can call `wallet.setOwner()` because it was set as the `owner` in the constructor.

<figure><img src="/files/UMpCal5kCpedBNs2zUFJ" alt=""><figcaption><p>testSetOwner()</p></figcaption></figure>

Write a fail test case:

```solidity
function testFailNotOwner() public {
    vm.prank(address(1));
    wallet.setOwner(address(1));
    assertEq(wallet.owner(), address(1));
}
```

`vm.prank(address(1))` means "for the next call, pretend we are `address(1)`". This test case would fail because `address(1)` is not the owner so that it cannot call `wallet.setOwner()`.

<figure><img src="/files/aCVqaBYlzwtiQUb8hFmB" alt=""><figcaption><p>testFailNotOwner()</p></figcaption></figure>

An extension of `vm.prank()` is the `vm.startPrank()` and `vm.endPrank()` pair. The impersonation will start on `vm.startPrank()` until `vm.endPrank()` is executed.

```solidity
function testFailSetOwnerAgain() public {
    // msg.sender = address(this)
    wallet.setOwner(address(1));

    vm.startPrank(address(1));

    // msg.sender = address(1)
    wallet.setOwner(address(1));
    wallet.setOwner(address(1));
    wallet.setOwner(address(1));

    vm.stopPrank();

    // msg.sender = address(this) -> revert because address(this) is not the owner anymore
    wallet.setOwner(address(1));
}
```

<figure><img src="/files/cWUfr5gPAzVJRsYvBtcX" alt=""><figcaption><p>testFailSetOwnerAgain()</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/authentication.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
